Citadel phone screen

citadel logo
citadel
June 10, 20254 reads

Summary

I had a phone screen with Citadel where I was asked to implement a Trie and its supporting functions, followed by an optimization question and a bonus question to find the longest prefix. I cleared the round.

Full Experience

Implement Trie and it's supporting functions -

* insert(word) - Insert a word in the trie,

  • search(word) - search if the word exists in the trie, and
  • startsWith(prefix) - search if there exist any word which starts with <prefix>.

Clarified if we are only dealing with lowercase alphabets. Yes.

I implemented
TrieNode
like below.

class TrieNode{
int val;
vector<TrieNode*> children;
};

Follow up question.
Currently, we are occupying 26 space for children. How can we optimize it ?
Suggested to use a Map instead of vector.

class TrieNode{
int val;
unordered_map<int, TrieNode*> child;
};

// int denotes 0 for 'a', 1 for 'b', or you can do char too in this case. // I chose int.


Looks good.

Bonus question.

Implement

longestPrefixWhichStartsWithPrefix(prefix) - find the longest prefix which starts with a given prefix.

prefix = abc
words = [abcdef, abcdeg] -> abcde

Cleared the round.

Interview Questions (2)

Q1
Implement Trie and its Supporting Functions
Data Structures & AlgorithmsMedium

Implement Trie and it's supporting functions -

* insert(word) - Insert a word in the trie,

  • search(word) - search if the word exists in the trie, and
  • startsWith(prefix) - search if there exist any word which starts with <prefix>.

Clarified if we are only dealing with lowercase alphabets. Yes.
Q2
Longest Prefix Which Starts With Prefix
Data Structures & AlgorithmsMedium

Implement

longestPrefixWhichStartsWithPrefix(prefix) - find the longest prefix which starts with a given prefix.

prefix = abc
words = [abcdef, abcdeg] -> abcde

Discussion (0)

Share your thoughts and ask questions

Join the Discussion

Sign in with Google to share your thoughts and ask questions

No comments yet

Be the first to share your thoughts and start the discussion!