Data Structures: Tries
Channel: HackerRank
Full video link:
https://www.youtube.com/watch?v=zIjfhVPRZCgData Structure • hard
Trie is a prefix tree where each edge represents a character and shared prefixes are stored once.
It is optimized for prefix queries, autocomplete, dictionary checks, and lexicographic traversal tasks.
Trie trades extra memory for fast prefix operations.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Insert/search prefix | O(length of word) |
Prefer video learning? This explainer gives a quick visual walkthrough of the core idea before you dive into the detailed sections below.
Data Structures: Tries
Channel: HackerRank
Full video link:
https://www.youtube.com/watch?v=zIjfhVPRZCgLearn the core building blocks and terminology in one place before comparisons, so the mechanics are clear and duplicates are removed.
Root node
What it is: Entry node with no character.
Why it matters: Root node is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
Children map
What it is: Character -> next node mapping.
Why it matters: Children map is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
End marker
What it is: Flag marking completed word.
Why it matters: End marker is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
Prefix path
What it is: Node path representing query prefix.
Why it matters: Prefix path is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
Optional payload
What it is: Metadata stored at terminal nodes.
Why it matters: Optional payload is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
Prefix
What it is: Starting substring of a word.
Why it matters: Starting substring of a word. In Trie (Prefix Tree), this definition helps you reason about correctness and complexity when inputs scale.
Terminal node
What it is: Node representing end of a valid word.
Why it matters: Node representing end of a valid word. In Trie (Prefix Tree), this definition helps you reason about correctness and complexity when inputs scale.
Compressed trie
What it is: Trie variant collapsing single-child paths.
Why it matters: Trie variant collapsing single-child paths. In Trie (Prefix Tree), this definition helps you reason about correctness and complexity when inputs scale.
Radix tree
What it is: Space-optimized trie with merged edges.
Why it matters: Space-optimized trie with merged edges. In Trie (Prefix Tree), this definition helps you reason about correctness and complexity when inputs scale.
Branching factor
What it is: Average number of children per node.
Why it matters: Average number of children per node. In Trie (Prefix Tree), this definition helps you reason about correctness and complexity when inputs scale.
This walkthrough connects the core concepts of Trie (Prefix Tree) into one end-to-end execution flow.
Step 1
Entry node with no character.
Before
After
Transition
Why this step matters: Root node is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
Step 2
Character -> next node mapping.
Before
After
Transition
Why this step matters: Children map is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
Step 3
Flag marking completed word.
Before
After
Transition
Why this step matters: End marker is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
Step 4
Node path representing query prefix.
Before
After
Transition
Why this step matters: Prefix path is a required building block for understanding how Trie (Prefix Tree) stays correct and performant on large inputs.
vs Hash map of full strings
When to choose this: Choose trie for frequent prefix queries.
Tradeoff: Hash map is simpler and often lighter for exact-match only.
vs Sorted array + binary search
When to choose this: Choose trie when many prefix checks are needed online.
Tradeoff: Sorted arrays can be more memory-efficient for static dictionaries.
vs Suffix array/tree
When to choose this: Choose trie for prefix-oriented lookups.
Tradeoff: Suffix structures target substring search across whole text.
Autocomplete-style systems rely on prefix indexing strategies closely related to trie behavior.
Takeaway: Prefix-first retrieval is the core value proposition.
VS Code ecosystem
Code completion engines maintain prefix-indexed symbol data for fast suggestion filtering.
Takeaway: Trie-like indexing improves developer UX latency.
Cloudflare
Domain and rule matching systems benefit from prefix tree structures for hierarchical matching.
Takeaway: Trie structures map naturally to hierarchical token spaces.
Implement nodes and child slots directly so prefix-tree behavior is fully explicit without built-in hash maps.
Complexity: Time O(L) per insert/search, L = word length
Trie insert + search
type TrieNode = { isWord: boolean; children: Array<TrieNode | null> }
function createTrieNode(): TrieNode {
return { isWord: false, children: new Array<TrieNode | null>(26).fill(null) }
}
function charIndex(char: string): number {
return char.charCodeAt(0) - 97
}
function insertWord(rootNode: TrieNode, word: string): void {
let currentNode = rootNode
for (let index = 0; index < word.length; index += 1) {
const childIndex = charIndex(word[index])
if (!currentNode.children[childIndex]) {
currentNode.children[childIndex] = createTrieNode()
}
currentNode = currentNode.children[childIndex]!
}
currentNode.isWord = true
}
function searchWord(rootNode: TrieNode, word: string): boolean {
let currentNode: TrieNode | null = rootNode
for (let index = 0; index < word.length && currentNode; index += 1) {
currentNode = currentNode.children[charIndex(word[index])]
}
return Boolean(currentNode?.isWord)
}
Use these signals to decide if this data structure/algorithm is the right fit before implementation.
O(length of query).| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Longest Common Prefix | Easy | O(n*m) |
| 2 | Implement Trie (Prefix Tree) | Medium | O(L) |
| 3 | Design Add and Search Words Data Structure | Medium | O(L) avg |
| 4 | Replace Words | Medium | O(total chars) |
| 5 | Map Sum Pairs | Medium | O(L) |
| 6 | Maximum XOR of Two Numbers in an Array | Medium | O(n * bits) |
| 7 | Word Search II | Hard | Pruned DFS |
| 8 | Palindrome Pairs | Hard | O(n*k^2) |
| 9 | Stream of Characters | Hard | O(L) per query |
| 10 | Word Squares | Hard | Backtracking + trie |