Suggest Three Products as a Customer Types a Search Query
Last updated: November 29, 2026
Quick Overview
Given a catalog of product names and a search word typed one character at a time, return after each keystroke up to three catalog names that start with the typed prefix, in lexicographic order.
Amazon
November 29, 20260
0
0 solved
Given a catalog of product names and a search word typed one character at a time, return after each keystroke up to three catalog names that start with the typed prefix, in lexicographic order.
An Amazon assessment favorite tied directly to search-box autocomplete. Graders look for sorting plus binary search on each prefix (or a trie that stores top suggestions), and an explanation of the trade-off between the two.
What the Interviewer Expects
- Sort the catalog once
- Binary search for each prefix's first match with bisect
- Take up to three consecutive matches
- Compare with a trie storing top three per node
- Handle prefixes with no matches
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you rank by popularity rather than alphabetically?
- How would this scale to a catalog of 300 million products?
- How would you support typo tolerance?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Products: list of lowercase strings, may contain duplicates (keep them). Search word: non-empty lowercase string. Return a list with one list of up to three suggestions per typed prefix....
High-Level Design
Sort products. For prefix p, bisect_left(products, p) gives the first index whose string is >= p, which is the first candidate that could start with p. Check the next three entries and keep those th...