What is Trie?
Trie (prefix tree) is a tree-like data structure for storing strings where each node represents a character. It enables O(m) search/insert where m is string length.Quick Recognition: Problems involving prefix matching, autocomplete, word dictionaries, or searching multiple words efficiently. Keywords: “prefix”, “dictionary”, “word search”, “autocomplete”.
Pattern Recognition Checklist
Use Trie When
- Prefix-based search (autocomplete)
- Word dictionary with wildcard search
- Word search in 2D grid (multiple words)
- Longest common prefix
- Spell checking
- IP routing (binary tries)
Don't Use When
- Exact match only (use HashSet)
- Single word search (use HashMap)
- Memory is very constrained
- Small dictionary (overhead not worth it)
Trie Complexity Reference
| Operation | Time | Space |
|---|---|---|
| Insert | O(m) | O(m) per word |
| Search | O(m) | - |
| Prefix search | O(m) | - |
| Delete | O(m) | - |
| Build trie | O(n * m) | O(n * m * 26) worst case |
When to Use
Autocomplete
Suggest words by prefix
Spell Checker
Find words with similar prefixes
Word Search
Search word in grid
Longest Prefix
Find longest common prefix
Basic Implementation
Pattern Variations
1. Word Search II (Backtracking + Trie)
2. Autocomplete System
Classic Problems
| Problem | Pattern | Key Insight |
|---|---|---|
| Implement Trie | Basic ops | HashMap children |
| Word Search II | Trie + DFS | Prune with trie |
| Autocomplete | Trie + Heap | Top-k by frequency |
| Longest Word | BFS/DFS | Check prefixes exist |
| Word Dictionary | Trie + ’.’ | DFS for wildcards |
Interview Problems by Company
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Implement Trie | All FAANG | Basic trie ops |
| Word Dictionary | Meta, Amazon | Wildcard DFS |
| Replace Words | Amazon | Prefix replacement |
| Map Sum Pairs | Trie with values |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I’ll use a Trie for efficient prefix-based operations.”
- “Each node has children (HashMap or array of 26).”
- “I’ll mark end-of-word with a boolean flag.”
- “Insert and search are both O(word length).”
- “For Word Search II, I’ll combine Trie with DFS backtracking.”
Trie Node Choices
Trie Node Choices
| Implementation | Pros | Cons |
|---|---|---|
| HashMap children | Flexible, sparse-friendly | Slightly slower |
| Array[26] | Faster access | Wastes memory for sparse |
| With count | Supports frequency | Extra space |
| With word | Store full word at end | Easier retrieval |
Common Optimizations
Common Optimizations
- Remove word from trie after finding (Word Search II)
- Store word at end node instead of just boolean
- Track count for frequency-based problems
- Prune empty branches after deletion
Practice Problems
Implement Trie
Basic trie operations
Word Search II
Backtracking with trie
Design Search Autocomplete
Trie with frequency
Replace Words
Prefix replacement
Practice Roadmap
1
Day 1: Basic Trie
- Solve: Implement Trie, Longest Common Prefix
- Focus: Insert, search, startsWith operations
2
Day 2: Pattern Matching
- Solve: Word Dictionary, Replace Words
- Focus: Wildcard handling, prefix matching
3
Day 3: Trie + DFS
- Solve: Word Search II
- Focus: Combining trie with backtracking
4
Day 4: Advanced Trie
- Solve: Autocomplete System, Palindrome Pairs
- Focus: Ranking, complex traversals