Trie and Heap Autocomplete System
Updated
Problem statement
Build a search autocomplete system as a hybrid data structure that combines a trie with a heap, and return the suggestions it shows after each character of keystrokes.
The search history starts with each sentences[i] searched times[i] times, and the current text starts empty. Then the characters of keystrokes are typed one at a time:
- A letter or a space is added to the current text. Suggest the history sentences that start with the current text, at most
3of them, from the most searched to the least. Sentences with the same count are listed in ASCII order, so a space comes before any letter. If no sentence matches, the list is empty. '#'ends the current text as a finished search. Add1to its count, or add it to the history with a count of1if it is new. The current text becomes empty again, and the list for'#'is empty.
Return the suggestion lists in the order the characters are typed.
Note: a trie (prefix tree) is a tree where each edge adds one character, so all sentences that share a prefix share the path for that prefix.
Function
autocomplete(sentences: String[], times: int[], keystrokes: String) → List<List<String>>Examples
Example 1
sentences = ["bid","buy","buy all","ask","bit"]times = [3,1,2,5,1]keystrokes = "buy#b"return = [["bid","buy all","bit"],["buy all","buy"],["buy all","buy"],[],["bid","buy","buy all"]]After b, four sentences match. bid has 3 searches and buy all has 2. bit and buy tie at 1, and bit comes first in ASCII order, so only bit fits. The # raises buy to 2 searches, so the last b puts it before buy all and drops bit.
Example 2
sentences = ["ab","a b"]times = [1,1]keystrokes = "a c#a"return = [["a b","ab"],["a b"],[],[],["a b","a c","ab"]]Every count is 1, so ASCII order decides, and a space comes before any letter. No sentence starts with a c, so that list is empty. The # saves a c, and it shows up for the last a.
Constraints
1 <= sentences.length <= 100times.length == sentences.length1 <= sentences[i].length <= 1001 <= times[i] <= 50- Each
sentences[i]is made of lowercase English letters with a single space between words and no leading or trailing space. Allsentences[i]are distinct. 1 <= keystrokes.length <= 5000keystrokescontains only lowercase English letters, spaces and'#'. Each'#'ends a non-empty sentence in the same format assentences[i].- The current text never has more than
200characters.
More Citadel Phone Screen questions
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Currency Arbitrage CyclePHONE SCREEN · Seen Sep 2026
- Order Book with Cancel by Order IDPHONE SCREEN · Seen Sep 2026
- Smart Queue of Latest PricesPHONE SCREEN · Seen Sep 2026
- Merge Price-Delta FeedsPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Design Search Autocomplete SystemRoblox · PHONE SCREEN · ONSITE INTERVIEW
- Design Search Autocomplete SystemBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Design a Food Rating SystemApple · PHONE SCREEN
- Design File SystemGoogle · PHONE SCREEN
- Long-ID Store Without HashingAdobe · PHONE SCREEN
- Implement TrieOptiver · PHONE SCREEN
- Word Dictionary with WildcardsDatadog · PHONE SCREEN
- Design a File System with PathsAirbnb · PHONE SCREEN
- Word Dictionary with Wildcard FindAmazon · ONSITE INTERVIEW
- Implement a TrieMicrosoft · ONSITE INTERVIEW
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences