Search Suggestions System
On this page
Problem statement
Given a list of product names and a search word typed one letter at a time, return, after each letter, up to three product names that start with the typed prefix, in alphabetical order. If fewer than three match, return those that do; if none match, return an empty list for that letter.
The typeahead box in miniature. It is a natural fit for a trie, and also a nice example of a simpler answer (sort plus binary search) that is just as fast here. Interviewers like to ask for both and then push on scale. Original problem: LeetCode 1268.
Example

products = [cable, camera, candle, canvas, cap, car, card], searchWord = "cand"
Output:
"c" -> [cable, camera, candle]
"ca" -> [cable, camera, candle]
"can" -> [candle, canvas]
"cand" -> [candle]
Constraints (assumed)
- Up to 1,000 products, each up to 3,000 lowercase letters; total length up to 2 × 10⁴. The search word is up to 1,000 letters.
What this question tests
- Building a trie and storing per-node answers (the top three) so each query step is O(1) to read.
- Or: using sorted order and binary search, since matches for a prefix form one contiguous block.
- Handling the moment the prefix stops matching anything.
Questions to ask first
- Is "alphabetical" plain string order? (Here: yes, lowercase letters.)
- Can products repeat? (Assume unique; with duplicates, decide whether to show both.)
- Will many search words be asked against the same products? That favors precomputing a trie.
- Should results be ranked by popularity instead? (Here: no. That is the common follow-up.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.