Search Suggestions System
Build incremental typeahead suggestions as a user types.
What This Problem Is Asking
In one sentence
Return up to three lexicographically smallest products for each search prefix.
Full problem statement
Search Suggestions System
You are given an array of string products and a string searchWord.
Design a system that suggests **at most three** product names from products after each character of searchWord is typed. Suggested products must share a common prefix with the substring of searchWord typed so far. If more than three products match that prefix, return the three **lexicographically smallest** strings.
Return a list of lists: the suggestions available after typing the first character, then after the second, and so on until the full searchWord has been typed.
Recognition Clues
Signals that this pattern likely applies.
- You need results for every prefix, not one final query.
- Results must be ordered and capped.
- Prefix filtering dominates the workflow.
Why This Pattern Fits
Prefix indexing and sorting keep each keystroke response predictable.
Daily usefulness: Core mental model for search bars, command palettes, and autocomplete.
Interview usefulness: Tests prefix data structures, ordering constraints, and incremental queries.
Real-world relevance: Mirrors product search, docs lookup, and in-app finder experiences.
Code Snippets
Practical Use Cases
- Product autocomplete in ecommerce search.
- Command palette query suggestions.
- Instant article lookup in docs portals.
Demo Ideas
Interactive ways to teach this algorithm.
- Animate suggestions updating on each keystroke.
- Compare strict prefix vs fuzzy fallback behavior.