A Trie is a tree-like data structure that stores strings by sharing common prefixes. Each node represents a character, and paths from root to leaves form words.
Click to reveal answer
beginner
Why can't a Hash Map efficiently handle prefix searches on strings?
Hash Maps store keys independently and do not share prefixes. To find all keys with a prefix, you must check every key, which is slow and inefficient.
Click to reveal answer
intermediate
How does a Trie improve prefix search compared to a Hash Map?
Trie nodes share prefixes, so searching for all words with a prefix only requires traversing the prefix path once, making prefix queries fast and efficient.
Click to reveal answer
beginner
What is a real-life example where Trie is better than Hash Map for strings?
Autocomplete in search engines uses Trie to quickly find all words starting with typed letters, which is hard to do efficiently with Hash Maps.
Click to reveal answer
intermediate
What is a limitation of Hash Maps when storing large sets of strings with common prefixes?
Hash Maps store each string separately, wasting memory by repeating common prefixes, while Trie stores shared prefixes once, saving space.
Click to reveal answer
What does a Trie node typically represent?
AA single character of a string
BA whole string
CA hash value
DAn integer index
✗ Incorrect
Each Trie node represents one character, building strings by linking nodes.
Why is prefix search inefficient in a Hash Map?
ABecause Hash Maps only store integers
BBecause Hash Maps do not store keys in order
CBecause Hash Maps store keys as linked lists
DBecause Hash Maps use trees internally
✗ Incorrect
Hash Maps store keys without order or prefix sharing, so prefix search requires checking all keys.
Which data structure is best for autocomplete features?
ATrie
BHash Map
CStack
DQueue
✗ Incorrect
Trie efficiently finds all words with a given prefix, ideal for autocomplete.
What is a key memory advantage of Trie over Hash Map for strings?
ATrie duplicates strings for speed
BTrie stores strings as integers
CTrie compresses strings into hashes
DTrie shares common prefixes to save space
✗ Incorrect
Trie nodes share prefixes, reducing repeated storage of common parts.
Which operation is faster in Trie compared to Hash Map?
AInserting integers
BFinding a key by exact match
CFinding all keys with a given prefix
DDeleting nodes
✗ Incorrect
Trie is optimized for prefix queries, unlike Hash Map.
Explain why Trie is used for prefix searches instead of Hash Maps.
Think about how strings are stored and searched.
You got /4 concepts.
Describe a real-world scenario where Trie is more efficient than Hash Map for string operations.
Consider typing in a search box.
You got /4 concepts.
Practice
(1/5)
1. Why do we use a Trie data structure instead of a Hash Map for storing many strings?
easy
A. Because Hash Map can only store numbers, not strings
B. Because Hash Map uses less memory than Trie
C. Because Trie stores strings as keys directly without splitting
D. Because Trie can efficiently find all strings with a common prefix
Solution
Step 1: Understand Trie prefix search ability
Trie stores strings character by character, allowing quick prefix-based searches.
Step 2: Compare with Hash Map capabilities
Hash Map stores whole strings as keys but cannot efficiently find all keys sharing a prefix.
Final Answer:
Because Trie can efficiently find all strings with a common prefix -> Option D
Quick Check:
Trie = prefix search [OK]
Hint: Trie excels at prefix search, Hash Map does not [OK]
Common Mistakes:
Thinking Hash Map can do prefix search efficiently
Confusing memory usage advantages
Believing Trie stores whole strings as keys
2. Which of the following is the correct way to declare a Trie node struct in Rust for lowercase English letters?
easy
A. struct TrieNode { children: [Option; 26], is_end: bool }
B. struct TrieNode { children: Vec<TrieNode>, is_end: bool }
C. struct TrieNode { children: HashMap, is_end: bool }
D. struct TrieNode { children: [TrieNode; 26], is_end: bool }
Solution
Step 1: Understand Rust ownership and recursive types
Recursive structs need Box to allocate on heap to avoid infinite size.
Step 2: Check array size and type
Array of 26 Option fits lowercase letters and allows empty children.
Strings "cat", "car", and "dog" are inserted, so prefixes "ca" and "do" exist.
Step 2: Check starts_with queries
"ca" matches "cat" and "car" (true), "do" matches "dog" (true), "bat" not inserted (false).
Final Answer:
true
true
false -> Option C
Quick Check:
Prefix search matches inserted prefixes [OK]
Hint: Inserted prefixes return true on starts_with [OK]
Common Mistakes:
Assuming starts_with returns true only for full words
Confusing starts_with with exact search
Mixing up true/false outputs
4. Given this Rust Trie insert function snippet, what is the bug?
fn insert(&mut self, word: &str) {
let mut node = &mut self.root;
for c in word.chars() {
let index = (c as usize) - ('a' as usize);
if node.children[index].is_none() {
node.children[index] = Some(Box::new(TrieNode::new()));
}
node = &mut node.children[index].as_mut().unwrap();
}
node.is_end = true;
}
medium
A. The index calculation assumes only lowercase letters without validation
B. The unwrap() call can cause a panic if children[index] is None
C. The loop should iterate over bytes, not chars
D. The is_end flag is set before the loop
Solution
Step 1: Analyze index calculation
Index is calculated by subtracting 'a' from character code, assuming lowercase letters only.
Step 2: Identify missing validation
No check if character is lowercase a-z, so uppercase or other chars cause wrong index or panic.
Final Answer:
The index calculation assumes only lowercase letters without validation -> Option A
Quick Check:
Index must be validated for safe access [OK]
Hint: Check character range before index calculation [OK]
Common Mistakes:
Ignoring character validation causes panic
Thinking unwrap() is unsafe here (it's safe after check)
Confusing iteration over chars vs bytes
5. You want to find all words in a dictionary that start with a given prefix efficiently. Why is a Trie better than a Hash Map for this task, and how does it achieve this?
hard
A. Trie uses less memory than Hash Map for storing strings; Hash Map stores full strings wasting space
B. Trie stores words character by character, enabling fast prefix traversal; Hash Map cannot efficiently list keys by prefix
C. Trie hashes prefixes for quick lookup; Hash Map hashes full strings only
D. Trie sorts all words internally; Hash Map stores words unsorted