Type-Ahead Prefix Matches
Updated
Problem statement
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 <= 1000000 <= words[i].length, prefix.length <= 100- Every word and the prefix contain only lowercase English letters.
More Navan interview questions
- Time Based Key-Value StoreONSITE INTERVIEW · Seen Dec 2025
- Minimum Steps on a Circular Character DialONSITE INTERVIEW · Seen Jan 2024
- Minimum Stick Connection CostONSITE INTERVIEW · Seen Jan 2024
- Shortest Substring with at Least K Distinct CharactersONSITE INTERVIEW · Seen Jan 2024
- Compare N-Ary Tree Leaf SequencesONSITE INTERVIEW · Seen Nov 2023
- Roman to IntegerPHONE SCREEN · Seen Oct 2024
- Sort ColorsPHONE SCREEN · Seen Oct 2024
- Basic CalculatorPHONE SCREEN · Seen Jun 2024
- LRU CachePHONE SCREEN · Seen Jun 2024
- House RobberPHONE SCREEN · Seen May 2024
Similar questions at other companies
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
- Count Word FrequenciesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Most Voted CandidateExpedia · ONSITE INTERVIEWExpedia interview questions
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions