Type-Ahead Prefix Matches
Reported by candidates from Navan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Navan reported this one in December 2024, and it looks easier than it is. Strip the wrapper and it's a filter, a dedupe, and a sort. The one-pass version takes five minutes to write. The follow-up is where the OA gets real: how does it hold up when the same prefix gets queried a million times? If you've got an invite for this, know both halves cold. StealthCoder is the safety net running invisibly during the live OA if your mind goes blank on the trie discussion.
The problem
Given a vocabulary array words and a string prefix, return every distinct vocabulary word that starts with prefix. Matching is case-sensitive. Return the results in lexicographic order. Interview follow-up The interview required actual working code and then asked how it performs at scale and how to optimize it. Discuss repeated-query indexing with a trie or a sorted vocabulary, index build and update costs, output-size costs, and invalidating cached results after vocabulary changes. This is a follow-up to the coding task. Function autocomplete(words: String[], prefix: String) → String[] Examples Example 1 words = ["dog","door","deer","doom","door"] prefix = "do" return = ["dog","doom","door"] The three distinct matching words are returned once in lexicographic order. Example 2 words = ["apple","banana"] prefix = "cat" return = [] No vocabulary word begins with cat. Constraints 0 <= words.length <= 100000 0 <= words[i].length, prefix.length <= 100 Every word and the prefix contain only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The baseline is simple. Loop through words, keep the ones where startsWith(prefix) is true, push them into a set to drop duplicates, then sort. That's O(n * L) for the scan plus O(k log k * L) for sorting the k matches. Pitfall one is forgetting duplicates, since example 1 repeats "door". Pitfall two is sorting the whole vocabulary on every call. The scale answer is to dedupe and sort once, then binary search for the first word >= prefix and walk forward while the prefix still matches. A trie works too, with DFS in alphabetical child order giving sorted output for free. Mention build cost, update cost, and that output size dominates query time. Cached results go stale when the vocabulary changes, so invalidate by prefix path or version stamp. If you blank on any of this live, StealthCoder is the hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Type-Ahead Prefix Matches cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Navan's OA.
Navan reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Type-Ahead Prefix Matches FAQ
What's the trick in the Navan type-ahead prefix problem?+
There's no hidden trick in the base task. Filter with startsWith, dedupe with a set, sort lexicographically. The empty prefix case matters: every word matches, so you return the whole deduped sorted vocabulary. The real test is the scale follow-up, so have your answer ready.
How do I handle duplicates and ordering correctly?+
Put matches in a set, or sort first and skip adjacent equal words. Then sort lexicographically. Since everything is lowercase letters, the default string comparison gives the right order. Example 1 shows "door" appearing twice in the input and once in the output.
Trie or sorted array for repeated queries?+
Both work. Sorted deduped array plus binary search is less code and uses less memory. A trie gives O(prefix length) to reach the node, then DFS collects results in order. Pick the sorted array if you want to finish fast, the trie if updates are frequent.
What should I say about cache invalidation?+
Say that any insert or delete can change results for every prefix of that word. Either clear affected prefix keys, which is at most 100 per word, or keep a vocabulary version number and discard cached entries with an older version. Keep the answer short and concrete.
How do I prepare in 48 hours?+
Write the brute force from memory, then the binary search version, then a basic trie with insert and collect. Practice explaining complexity out loud: scan cost, build cost, query cost, and output size. That covers the coding task and the follow-up discussion.