2Brute2Best
🔥0

Search Suggestions System

Medium·LeetCode #1268 Pro OnlyInterview-Level
LeetCode
01

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.

Input

products, a list of product names, and searchWord, the word the user types.

Output

A list of lists. The list at position i holds the suggestions after the first i + 1 letters of searchWord are typed.

Example 1
Input: products = ["mobile", "mouse", "moneypot", "monitor", "mousepad"], searchWord = "mouse"
Output: [["mobile", "moneypot", "monitor"], ["mobile", "moneypot", "monitor"], ["mouse", "mousepad"], ["mouse", "mousepad"], ["mouse", "mousepad"]]

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.

Example 2
Input: products = ["havana"], searchWord = "havana"
Output: [["havana"], ["havana"], ["havana"], ["havana"], ["havana"], ["havana"]]

havana is the only product, and every typed prefix of it matches, so it is suggested after every letter.

Constraints
  • • 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
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“suggest after each letter”
→ Each answer is for a prefix of the typed word, and the prefixes are nested. The node for each prefix is one step below the node before it.
“the three smallest”
→ Sort first, so the alphabetically smallest products come first, and keep at most three at each node.
“products with a common prefix”
→ Products that share a prefix share the trie nodes for that prefix, so each node can hold its own short list.
03

Approach Complexity Ladder

Brute Force — filter, sort, take three, for each prefixO(m · (S + n log n · L))O(S)

For each typed prefix, keep the matching products, sort them and take the first three

Better — sort once, then lower_bound for each prefixO(n log n · L + m · L · log n)O(S)

Sort the products once; for each prefix, binary-search the first match and take the next three that still match

Optimal — trie that stores the three best at each nodeO(n log n · L + S + m · L)O(26 · S)

Insert sorted products into a trie; each node keeps up to three; one walk per typed letter

Brute Force Intuition

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:

Insert each word letter by letter; search by walking the same path.
Remember this · Trie
Trigger
Many words + prefix search, autocomplete, word dictionary with wildcards, maximum XOR.
Think
Do many queries share prefixes that I keep re-checking?
Key question
What does each node store (children, end-of-word, count)?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Trie translate into production architectures and low-level memory mechanics.