← All patterns
31 / 32
Strings & Tries

Trie

When you see it

Signal words

If the prompt uses any of these, this pattern should come to mind first.

prefixautocompletedictionaryword searchstartsWithwildcard
Practice

Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.

Ask yourself

  1. Are many words queried by prefix?
  2. Is startsWith or dictionary pruning repeated?
  3. Would a tree of characters avoid repeated string scans?

Classic problem

Implement Trie (LeetCode 208) — Insert words letter by letter; shared prefixes reuse existing nodes. Words end at marked isWord nodes.

Input: insert "car", "cat", "dog"

Output: search("car") = true, search("ca") = false, startsWith("ca") = true — the trie stores all three words, sharing the c→a path for “car” and “cat”.

Watch it run

Common pitfalls

Forgetting the terminal marker for words that are also prefixes.

Creating substrings repeatedly instead of moving by index.