Trie Pattern
The Trie (Prefix Tree) pattern stores a set of strings in a rooted tree where each edge spells one character and each path from the root spells a prefix. Insertions and lookups run in O(L) time, where L is the word length, independent of how many words the trie holds. It powers fast prefix queries, autocomplete, and word-search-on-a-grid problems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Complexity
- Time
- O(L)
- Space
- O(N * L)
When to use Trie
- You need to answer many prefix queries against a fixed dictionary (autocomplete, spell check, longest prefix match).
- You are searching a board or grid for any word from a list — a trie lets you abandon a path the moment the prefix is no longer in the dictionary.
- You need a fast 'word exists' or 'prefix exists' check across a large word list, where a hash set would lose the prefix structure.
- The problem asks for the shortest unique prefix, longest common prefix, or words that share a stem.
- You need to support insertions and deletions on a string set with stable O(L) cost regardless of dictionary size.
Template
class TrieNode {
constructor() {
this.children = new Map();
this.isWord = false;
}
}
class Trie {
constructor() {
this.root = new TrieNode();
}
insert(word) {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) node.children.set(ch, new TrieNode());
node = node.children.get(ch);
}
node.isWord = true;
}
search(word) {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) return false;
node = node.children.get(ch);
}
return node.isWord;
}
}Example LeetCode problems
| # | Problem | Difficulty | Frequency |
|---|---|---|---|
| 208 | Implement Trie (Prefix Tree) | medium | foundational |
| 211 | Design Add and Search Words Data Structure | medium | company favorite |
| 212 | Word Search II | hard | company favorite |
| 648 | Replace Words | medium | frequently asked |
| 720 | Longest Word in Dictionary | medium | foundational |
| 677 | Map Sum Pairs | medium | classic |
| 421 | Maximum XOR of Two Numbers in an Array | medium | company favorite |
Common mistakes
- Forgetting to set isWord (or the equivalent terminal flag) at the final node, so search() returns false for inserted words.
- Reusing a single TrieNode object instance across multiple children due to a default-argument bug or shared mutable default, which corrupts the entire tree.
- On Word Search II, building the trie but still issuing one DFS per dictionary word instead of a single board-wide DFS that walks the trie alongside the grid.
- Storing children in a fixed-size 26-element array even when the input includes uppercase, digits, or unicode — collisions truncate the trie silently.
- Failing to prune visited dictionary words after a match in Word Search II, which produces duplicate entries in the output list.
Frequently asked questions
When should I use a Trie vs a Hash Set?
Use a hash set when you only need 'does this exact string exist'. Use a trie when you need prefix queries, ordered enumeration of words that share a stem, or the ability to abandon a search as soon as a prefix is no longer viable (Word Search II, autocomplete).
How much memory does a Trie use?
Worst case O(N * L * alphabet) for N words of average length L. In practice, shared prefixes make tries much smaller than that bound. Use a hash map for children instead of a fixed array when the alphabet is large or sparse to avoid wasted slots.
How does Word Search II combine a Trie with DFS on a grid?
Build a trie from the dictionary, then DFS each board cell with a pointer into the trie. At each step, advance the trie pointer to the child for the current letter; if no child exists, prune. When you land on a trie node with isWord = true, record the path as a found word and unflag it to avoid duplicates.
Can a Trie support deletion?
Yes — walk down to the terminal node, clear the isWord flag, and walk back up removing any node that has no children and is not itself a word terminus. This keeps the trie shrunk to the minimal shape that supports the remaining words.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill the Trie pattern under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →