Efficient prefix matching, autocomplete, and dictionary lookups
51 problems·4 Easy·26 Medium·21 Hard
Pattern Study Guide & Cheat Sheet· O(L) per insert/search where L is word length · O(total characters * alphabet size)
▼
Tree structure where paths from root represent string prefixes, allowing prefix searches, autocomplete, and dictionary lookups in O(L) time proportional to word length.
Core Invariant: Each node represents a character transition. A node contains children `char -> TrieNode` and a boolean flag `is_end = True` indicating if a complete word terminates at that node.
Recognize it (Keywords & Signals)
Prefix search / implement startsWith(prefix)
Word search board with dictionary / Boggle game
Autocomplete / search suggestions / spell checker
Replace words with shortest root
Maximum XOR pair in an array (Binary Bitwise Trie)
When NOT to use
Exact lookups only with no prefix queries (standard Hash Set has less memory overhead and O(1) expected time).
How to solve (Step-by-step)
1.Define `TrieNode` with `children = {}` and `is_end = False`.
2.Insert: walk character by character. If character not in `curr.children`, create new node. Mark last node `is_end = True`.
3.Search: walk characters. If any character missing, return False. If all found, return `curr.is_end`.
4.StartsWith: walk characters. If any missing, return False. If found, return True regardless of `is_end`.
Memory explosion: in Python, using `dict` children is more memory-efficient than `[None] * 26`
Word Search II TLE: forget to remove found words from Trie or mark already visited grid cells
Prefix Tree (Trie) Implementation
class TrieNode:
def __init__(self):
self.children: dict[str, TrieNode] = {}
self.is_end: bool = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word: str) -> None:
curr = self.root
for char in word:
if char not in curr.children:
curr.children[char] = TrieNode()
curr = curr.children[char]
curr.is_end = True
def search(self, word: str) -> bool:
node = self._traverse(word)
return node is not None and node.is_end
def starts_with(self, prefix: str) -> bool:
return self._traverse(prefix) is not None
def _traverse(self, prefix: str) -> TrieNode | None:
curr = self.root
for char in prefix:
if char not in curr.children:
return None
curr = curr.children[char]
return curr
Cost
O(L) per insert/search where L is word length · O(total characters * alphabet size) (Lookup speed is completely independent of the number of words stored in the Trie.)
Canonical problems
#208 Implement Trie (Prefix Tree):Foundational insert, search, and startsWith operations
#211 Design Add and Search Words Data Structure:DFS on Trie branches to handle "." wildcard queries
#212 Word Search II:Traverse 2D matrix with Trie to prune invalid prefixes immediately
#421 Maximum XOR of Two Numbers in an Array:Bitwise 0/1 Trie picks opposite bits greedily
What is the Trie (Prefix Tree) pattern in coding interviews?
Efficient prefix matching, autocomplete, and dictionary lookups It is categorized under Data Structures and features 51 curated interview problems across Easy (4), Medium (26), and Hard (21) difficulties.
Which tech companies frequently test Trie (Prefix Tree)?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.