If you were building the autocomplete for a search box, how would you use a trie to return suggestions for what the user has typed so far?

technical-conceptual · Junior level · software-engineering

What the interviewer is really asking

Assesses whether the candidate can apply a trie to the autocomplete problem — navigate to the prefix node, traverse the subtree to collect completions, and reason about ranking and scale.

What to say

What to avoid

Example answers

Strong: Two steps. First I walk the trie from the root following each character the user has typed; that lands me at the node representing the prefix in O(L), or tells me no word has that prefix so I return nothing. Second, I traverse the subtree under that node — a DFS collecting every marked word-end below it — and those are the completions. For a real search box I wouldn't return all of them: I'd store a frequency or popularity score at each word and return the top few, so 'app' suggests the most-searched terms, not a random subtree order.

Weak: I'd store all the words in a trie and then search for the prefix the user typed; if it's in the trie I show it. I think that gives the suggestions.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.