Search Suggestions System
Problem Statement
You get a list of product names, products, and a word, searchWord, that the user types one letter at a time. After each letter, suggest up to three products that start with everything typed so far. If more than three products match, suggest the three that come first in alphabetical order.
Return one list of suggestions for each letter typed.
products, a list of product names, and searchWord, the word the user types.
A list of lists. The list at position i holds the suggestions after the first i + 1 letters of searchWord are typed.
Sorted, the products are mobile, moneypot, monitor, mouse, mousepad. After m and mo, all five products match, so the first three in alphabetical order are shown. After mou, mous and mouse, only mouse and mousepad match.
havana is the only product, and every typed prefix of it matches, so it is suggested after every letter.
- • 1 ≤ products.length ≤ 1000
- • 1 ≤ products[i].length ≤ 3000
- • 1 ≤ sum of all products[i].length ≤ 2 · 10^4
- • All strings in products are unique
- • products[i] uses only lowercase letters
- • 1 ≤ searchWord.length ≤ 1000
- • searchWord uses only lowercase letters
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each typed prefix, keep the matching products, sort them and take the first three
Sort the products once; for each prefix, binary-search the first match and take the next three that still match
Insert sorted products into a trie; each node keeps up to three; one walk per typed letter
For each prefix of searchWord (length 1, 2, 3 and so on), scan all products and keep the ones that start with the prefix. Sort the kept products and take at most three. Repeat for the next letter.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Trie translate into production architectures and low-level memory mechanics.