Bonus Day 99MediumLeetCode

Search Suggestions System

As each letter of a search word is typed, list up to three products, in alphabetical order, that start with what has been typed so far.

Pattern
Trie storing top-3 at each node
Time box
25 min
Time
O(n log n · L + S + m), S = total product letters, m = search word length
Space
O(S)

Try it first — give it 25 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    You get a list of distinct lowercase products and a searchWord. Imagine typing searchWord into a search box, one letter at a time. After each letter, show up to three products that start with the letters typed so far. If more than three match, show the three that come first alphabetically (lexicographically).

    Return a list with one entry per typed letter: len(searchWord) lists in total.

    Example: products ["mobile", "mouse", "moneypot", "monitor", "mousepad"], search word "mouse".

    TypedMatches, sortedShown
    mmobile, moneypot, monitor, mouse, mousepadmobile, moneypot, monitor
    mosame fivemobile, moneypot, monitor
    moumouse, mousepadmouse, mousepad
    mousmouse, mousepadmouse, mousepad
    mousemouse, mousepadmouse, mousepad

    Start with brute force

    Sort the products once. Then for each of the m prefixes, scan the whole sorted list and take the first three that start with the prefix.

    That's O(m · n · L) after sorting: each prefix check can compare up to L characters across n products. With n = 1,000 products and a 1,000-letter search word, that's 10⁶ prefix checks, each possibly long. It passes on small inputs but does the same work again for every letter typed.

    Building the intuition

    Every prefix you type is a path from the root of a trie. All products starting with mou live in the subtree under the node for mou. So each answer is "the three smallest words in some subtree".

    Finding the three smallest words in a subtree on every keystroke would mean a DFS each time. Can we precompute it instead?

    Here's the trick: insert products in sorted order. When mobile is inserted, it passes through m, mo, mob, …. Then moneypot passes through m and mo. Then monitor. Words arrive alphabetically, so the first three words to pass through a node are exactly that node's three alphabetically smallest words.

    So each node just keeps a little list, and appends a word only while the list has fewer than three entries. After building, every answer is ready and waiting at its node.

    The second observation makes the query side trivial: once the typed prefix leaves the trie (no product has it), every longer prefix also has no products. From then on, every answer is [].

    The optimal approach

    1. Sort products alphabetically.
    2. Build the trie. For each product (with its index idx in sorted order), walk its letters from the root, creating missing nodes. At every node you pass through, if len(node.top) < 3, append idx.
    3. Answer the queries. Start at the root. For each letter of searchWord:
      • if we're still in the trie, move to that letter's child (which may be missing);
      • if we're in the trie, the answer is the products at node.top; otherwise [].

    The subtle detail: append to top at each node after stepping into it, not at the root. The root would be the empty prefix, which isn't one of the answers. Storing indices instead of copies of the strings keeps memory small, especially in C++.

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    class TrieNode:
        def __init__(self):
            self.children = {}   # letter -> TrieNode
            self.top = []        # indices of the (at most 3) smallest products below this node
    
    
    class Solution:
        def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
            # Insert in sorted order, so the first 3 words to pass through a node
            # are exactly its 3 lexicographically smallest completions
            products.sort()
    
            root = TrieNode()
            for idx, word in enumerate(products):
                node = root
                for ch in word:
                    if ch not in node.children:
                        node.children[ch] = TrieNode()
                    node = node.children[ch]
                    if len(node.top) < 3:
                        node.top.append(idx)
    
            result = []
            node = root
            for ch in searchWord:
                # Once the typed prefix leaves the trie, it stays gone for every longer prefix
                if node is not None:
                    node = node.children.get(ch)
                result.append([] if node is None else [products[i] for i in node.top])
            return result

    Dry run

    Sorted products: 0 mobile, 1 moneypot, 2 monitor, 3 mouse, 4 mousepad. The top lists after building:

    NodeWords passing through, in ordertop (first 3)
    m0, 1, 2, 3, 4[0, 1, 2]
    mo0, 1, 2, 3, 4[0, 1, 2]
    mou3, 4[3, 4]
    mous3, 4[3, 4]
    mouse3, 4[3, 4]

    Walking "mouse" reads exactly these rows: [mobile, moneypot, monitor] twice, then [mouse, mousepad] three times.

    With products = ["havana"] and search word "tatiana": the root has no t child, so node becomes null on the first letter, and all seven answers are [].

    Complexity

    • Time: O(n log n · L + S + m). Sorting compares strings of length up to L. Building visits each of the S total letters once. Answering is one step per typed letter, plus copying at most 3 words per answer.
    • Space: O(S) for the trie nodes. Each node stores at most 3 indices, so the lists add only O(S) too.

    Edge cases and traps

    • Forgetting to sort first. The "first three to pass through" are then just the first three in the input, not the smallest.
    • Stopping the output early when no match exists. The answer always has len(searchWord) entries. Pad with empty lists.
    • Walking from a null node. Once you've fallen off the trie, keep node null and skip the lookup. Don't crash on null.children.
    • A product equal to a prefix. If mouse is a product and you've typed mouse, it's included, and it sorts before mousepad, because a prefix comes first alphabetically.
    • Storing all words at every node. That costs O(n · L) per node in the worst case. Cap the list at 3.

    Say it in the interview

    "Each typed prefix is a node in a trie of the products, and I need the three smallest words under it. If I sort the products first and insert them in that order, the first three words passing through any node are its three smallest, so each node just keeps up to three indices. Then I walk the search word through the trie and read off each node's list. Once I fall off the trie, every remaining answer is empty. That's the sort, plus linear work in the total number of letters."

    Follow-ups interviewers ask

    • "Can you do it without a trie?" Yes: sort, then for each prefix binary-search for the first product ≥ the prefix and check the next three. Even simpler, keep two pointers lo and hi over the sorted list and narrow them as each letter is typed.
    • "What if products have popularity scores?" Keep the top 3 by score at each node instead of the first 3. Updates now need a small heap or a re-sort per node.
    • "Products are added while users type?" The trie handles inserts in O(L), but each node's top list must be updated with a comparison instead of a simple append.

    Practise the same idea

    This is a bonus day of the free DSA plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free