A Trie is a tree-like data structure used to store a dynamic set of strings where keys are usually strings. Each node represents a character, and paths from root to leaves represent words.
Click to reveal answer
beginner
How does a Trie help in word search?
A Trie allows fast prefix-based search by traversing nodes corresponding to each character of the word. If traversal reaches the end of the word and the node marks a word end, the word exists.
Click to reveal answer
beginner
In a Trie, what does it mean if a node's 'is_end_of_word' flag is true?
It means that the path from the root to this node forms a complete word stored in the Trie.
Click to reveal answer
intermediate
What is the time complexity of searching a word in a Trie?
The time complexity is O(m), where m is the length of the word being searched, because we check each character once.
Click to reveal answer
intermediate
Why is a Trie more efficient than a list for searching words?
Because Trie searches by characters and prefixes, it avoids checking every word fully, making searches faster especially for large word sets.
Click to reveal answer
What does each node in a Trie typically represent?
AA sentence
BA whole word
CA number
DA character of a word
✗ Incorrect
Each node in a Trie represents a single character of a word.
What flag indicates that a node marks the end of a word in a Trie?
Ais_end_of_word
Bis_word_start
Cis_leaf_node
Dis_root
✗ Incorrect
The 'is_end_of_word' flag marks that the node completes a stored word.
What is the main advantage of using a Trie for word search?
ASorts words automatically
BUses less memory
CFaster prefix search
DStores numbers efficiently
✗ Incorrect
Tries allow fast prefix-based searches by traversing character nodes.
If a word is not found in a Trie, what happens during search?
AThe Trie deletes the word
BTraversal reaches a null or missing child node
CTraversal completes successfully
DThe search returns the closest word
✗ Incorrect
If a character node is missing, the word does not exist in the Trie.
What is the time complexity to search a word of length m in a Trie?
AO(m)
BO(1)
CO(n)
DO(m*n)
✗ Incorrect
Searching takes O(m) time, where m is the word length, as each character is checked once.
Explain how a Trie stores words and how you search for a word in it.
Think about walking down a tree following letters.
You got /4 concepts.
Describe the advantages of using a Trie for word search compared to a list of words.
Consider how searching by letters helps.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a Trie data structure in word search problems?
easy
A. To sort words alphabetically
B. To store words in a hash table
C. To efficiently store and search words by their prefixes
D. To perform arithmetic operations on words
Solution
Step 1: Understand Trie structure
A Trie stores characters in nodes where each path from root to a node represents a prefix of words.
Step 2: Purpose in word search
This structure allows quick prefix checks and word searches, making it efficient for word search problems.
Final Answer:
To efficiently store and search words by their prefixes -> Option C
Quick Check:
Trie = Prefix search [OK]
Hint: Trie is best for prefix-based word searches [OK]
Common Mistakes:
Confusing Trie with hash tables
Thinking Trie sorts words
Assuming Trie stores whole words only
2. Which of the following is the correct Rust syntax to define a Trie node struct with a children field as a vector of optional boxed Trie nodes?
easy
A. struct TrieNode { children: Vec> }
B. struct TrieNode { children: Vec<TrieNode> }
C. struct TrieNode { children: Option> }
D. struct TrieNode { children: Box> }
Solution
Step 1: Understand Rust ownership and pointers
To allow recursive data structures, Rust uses Box to allocate on heap and Option for nullable children.
Step 2: Correct syntax for children field
Children should be a vector of optional boxed TrieNode references: Vec>.
Final Answer:
struct TrieNode { children: Vec> } -> Option A
Quick Check:
Recursive struct uses Box and Option [OK]
Hint: Use Box and Option for recursive Trie nodes in Rust [OK]
Common Mistakes:
Using Vec<TrieNode> causes infinite size error
Wrapping Vec in Option instead of elements
Incorrect Box placement
3. Given the following Rust code snippet for searching a word in a Trie, what will be the output printed?
struct TrieNode {
children: Vec>,
is_end: bool,
}
impl TrieNode {
fn search(&self, word: &str) -> bool {
let mut node = self;
for ch in word.chars() {
let index = (ch as usize) - ('a' as usize);
match &node.children[index] {
Some(child) => node = child,
None => return false,
}
}
node.is_end
}
}
fn main() {
let mut root = TrieNode {
children: vec![None; 26],
is_end: false,
};
println!("{}", root.search("cat"));
println!("{}", root.search("car"));
}
medium
A. true
false
B. false
false
C. true
true
D. Compilation error
Solution
Step 1: Analyze search function behavior
The search returns true if all characters are found and the last node marks end of a word.
Step 2: Check given calls
No words are inserted; root.children initialized to vec![None; 26]. For "cat" and "car", first char 'c' (index 2) is None, so both return false.
Final Answer:
false
false -> Option B
Quick Check:
Search returns false for non-inserted words [OK]
Hint: Search returns true only if full word exists [OK]
Common Mistakes:
Assuming partial prefix returns true
Ignoring is_end flag
Confusing character indexing
4. Identify the bug in this Rust Trie search method and select the correct fix:
fn search(&self, word: &str) -> bool {
let mut node = self;
for ch in word.chars() {
let index = (ch as usize) - ('a' as usize);
if node.children[index].is_some() {
node = &node.children[index].unwrap();
} else {
return false;
}
}
true
}
medium
A. Return node.is_end instead of true at the end
B. Change unwrap() to expect() with error message
C. Use mutable reference for node variable
D. Initialize children vector with 26 None elements
Solution
Step 1: Understand search logic
The method should return true only if the word ends at a node marked as end of a word.
Step 2: Identify bug in return statement
Returning true always ignores if the node marks end of a word; must return node.is_end.
Final Answer:
Return node.is_end instead of true at the end -> Option A
Quick Check:
Search must check is_end flag [OK]
Hint: Always check is_end flag after traversal [OK]
Common Mistakes:
Returning true without checking is_end
Ignoring None cases
Misusing unwrap without safety
5. You want to implement a function in Rust that searches for all words in a Trie starting with a given prefix. Which approach correctly combines Trie traversal and word collection?
hard
A. Use a hash map to map prefixes to words and lookup directly
B. Search each word in Trie individually to check prefix match
C. Store all words in a vector and filter by prefix using string starts_with
D. Traverse Trie nodes for prefix, then recursively collect all words from that node
Solution
Step 1: Traverse Trie for prefix
Follow nodes for each character in prefix to reach prefix node.
Step 2: Collect words recursively
From prefix node, recursively explore children to collect all complete words.
Final Answer:
Traverse Trie nodes for prefix, then recursively collect all words from that node -> Option D
Quick Check:
Prefix traversal + recursive collection = A [OK]
Hint: Find prefix node, then gather words recursively [OK]
Common Mistakes:
Checking all words instead of prefix traversal
Using external data structures unnecessarily
Ignoring recursive collection after prefix
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.