Search Suggestions with a Prefix Tree
Updated
Problem statement
You are given an array of distinct lowercase product names products and a lowercase search string query.
After each character of query is typed, form the accumulated non-empty prefix and return up to three product names that begin with that prefix.
- Order matching products lexicographically.
- If more than three products match, keep the first three.
- Once a prefix has no matches, that prefix and every longer prefix return an empty list.
Return one suggestion list for every character of query, in order.
Function
searchSuggestions(products: String[], query: String) → String[][]Examples
Example 1
products = ["mobile","mouse","moneypot","monitor","mousepad"]query = "mouse"return = [["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]The first two prefixes are m and mo, whose first three lexicographic matches are mobile, moneypot, and monitor. From mou onward, only mouse and mousepad match.
Example 2
products = ["bags","baggage","banner","box","cloths"]query = "bags"return = [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]The prefixes b and ba have at least three matches. Prefix bag has two matches, and the full prefix bags has one.
Example 3
products = ["alpha","alpine","beta"]query = "cat"return = [[],[],[]]No product begins with c, so every prefix returns an empty list.
Constraints
1 <= products.length <= 100000.- Every product is a distinct non-empty lowercase English string.
1 <= query.length <= 100000.- The total number of characters across all products is at most
10^6.
More Citadel Onsite Interview questions
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Removal PoolONSITE INTERVIEW · Seen Sep 2026
- Centered K-by-K Submatrix SumsONSITE INTERVIEW · Seen Aug 2026
- Traverse Interior Matrix Cells in Row-Major OrderONSITE INTERVIEW · Seen Aug 2026
- Limit Order Book Matching EnginePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Fixed-Capacity Circular BufferONSITE INTERVIEW · Seen Jun 2026
- Stock Trading with a Fee and a CooldownONSITE INTERVIEW · Seen May 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2026
- 2048 Simulation and Board CodecONSITE INTERVIEW · Seen Jan 2026
Similar questions at other companies
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Shell Path AutocompleteFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Remove Prefix StringsRoblox · PHONE SCREENRoblox interview questions
- Every Distinct Substring in OrderAdobe · PHONE SCREENAdobe interview questions
- Top K Query SuggestionsMicrosoft · PHONE SCREENMicrosoft interview questions
- Find the Words for Each PrefixDocuSign · PHONE SCREENDocuSign interview questions
- Suggested ProductsSquarepoint Capital · OASquarepoint Capital interview questions
- Find All Word Occurrences in TextSentry · PHONE SCREENSentry interview questions