cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 99 of 218
Ideas this builds on
Tries99 / 218

A prefix finds a subtree; autocomplete explores it

Picture

Typing ca into autocomplete does not require reconsidering every stored word from the beginning. Follow c, then a to reach one trie node. Every completion is a terminal node somewhere below that prefix. Lookup narrows the search; traversal produces the answers.

Predict

If the prefix is only one character long, must an autocomplete query take constant time even when it returns a million words?

Reveal the reasoning

No. Reaching the prefix can be quick, but enumerating and constructing a million outputs still takes work proportional to those outputs. Distinguish finding a matching region from reporting everything inside it.

Go deeper: build outputs from terminal nodes

Use the dictionary trie from card 16, with None marking a complete word. Collect in lexical order by exploring sorted character edges.

def autocomplete(root, prefix):
    node = root
    for char in prefix:
        if char not in node:
            return []
        node = node[char]
    answers, path = [], list(prefix)
    def collect(current):
        if None in current:
            answers.append("".join(path))
        for char in sorted(key for key in current if key is not None):
            path.append(char)
            collect(current[char])
            path.pop()
    collect(node)
    return answers

trie = {"c": {"a": {"r": {None: True, "t": {None: True}}, "t": {None: True}}}}
assert autocomplete(trie, "ca") == ["car", "cart", "cat"]
assert autocomplete(trie, "cart") == ["cart"]
assert autocomplete(trie, "dog") == []

If the prefix has length P, the matching subtree has S nodes, and output strings contain Z characters total, traversal takes O(P+S+Z) work for a fixed bounded alphabet. Sorting children adds the corresponding per-node sorting cost for an unrestricted alphabet. Output storage is O(Z); the working path and recursion use space proportional to maximum word length, with temporary child-key lists also contributing.

For popularity-ranked top-k suggestions, lexical DFS is insufficient. Options include traversing matches and selecting the best k, or maintaining top-k summaries at prefix nodes. The latter accelerates queries but adds storage and update work when popularity changes.

Analogy limit: a prefix tree organizes which strings match; it does not intrinsically know relevance, spelling correction, or personalized ranking.

Change one rule

Only return the five most popular matching words. Is stopping this lexical traversal after five outputs correct?

Check the variation

No. Lexical order and popularity order are different. You need ranking information and an algorithm that can justify excluding unseen candidates.

Connect

Heaps can help select ranked candidates. Board search uses the same prefix subtree as a constraint on movement. Recognition matches representation to query workload.