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 down the tree represent words.
Click to reveal answer
beginner
How does the search operation work in a Trie?
Search starts at the root and follows nodes matching each character of the search word. If all characters are found and the last node marks the end of a word, the search is successful.
Click to reveal answer
beginner
What does it mean if a node in a Trie has a 'is_end_of_word' flag set to 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
Why is Trie search operation efficient for prefix matching?
Because it shares common prefixes among words, the search only traverses nodes for the prefix characters once, making prefix queries fast.
Click to reveal answer
intermediate
In Rust, what is a common way to represent children nodes in a Trie?
Children nodes are often stored in a fixed-size array or a hash map keyed by characters, allowing quick access to the next node for a given character.
Click to reveal answer
What does the Trie search operation return if the word is not found?
AFalse or None
BTrue
CThe last node visited
DThe root node
✗ Incorrect
If the word is not found, the search returns false or None indicating the word does not exist in the Trie.
Which of these is NOT a step in Trie search?
ATraverse nodes matching each character
BReturn true if all characters matched and end flag is true
CCheck if the last node marks end of word
DSort the children nodes before searching
✗ Incorrect
Sorting children nodes is not required in Trie search; direct access is used.
What data structure is commonly used to store children nodes in a Trie in Rust?
AHashMap or array
BLinked list
CStack
DQueue
✗ Incorrect
HashMap or fixed-size arrays keyed by characters are common for fast access to children nodes.
If a search word is a prefix of a longer word in the Trie but not marked as a complete word, what does search return?
APartial match node
BFalse
CTrue
DError
✗ Incorrect
Search returns false because the word is not marked as a complete word even if prefix nodes exist.
What is the time complexity of searching a word of length n in a Trie?
AO(n^2)
BO(log n)
CO(n)
DO(1)
✗ Incorrect
Search time is proportional to the length of the word, O(n), as each character is checked once.
Explain step-by-step how the search operation works in a Trie.
Think about walking down the tree following characters.
You got /6 concepts.
Describe how children nodes are stored and accessed in a Rust Trie implementation.
Consider how to find the next letter node quickly.
You got /4 concepts.
Practice
(1/5)
1. What does the search operation in a Trie data structure do?
easy
A. Counts the total words stored in the Trie
B. Inserts a new word into the Trie
C. Checks if a given word exists in the Trie
D. Deletes a word from the Trie
Solution
Step 1: Understand the purpose of search in Trie
The search operation is designed to check if a specific word is present in the Trie structure.
Step 2: Differentiate from other operations
Insertion adds words, deletion removes words, and counting totals words, but search only checks existence.
Final Answer:
Checks if a given word exists in the Trie -> Option C
Quick Check:
Search = Check existence [OK]
Hint: Search checks presence, not insertion or deletion [OK]
Common Mistakes:
Confusing search with insert operation
Thinking search deletes words
Assuming search counts words
2. Which of the following is the correct Rust function signature for a Trie search method that returns a boolean?
easy
A. fn search(&self, word: &str) -> bool
B. fn search(word: String) -> bool
C. fn search(&mut self, word: &str) -> String
D. fn search(&self, word: &str) -> usize
Solution
Step 1: Identify correct method receiver and parameters
The search method should borrow self immutably (&self) and take a string slice (&str) for the word to search.
Step 2: Confirm return type
Search returns a boolean indicating presence, so return type must be bool.
Final Answer:
fn search(&self, word: &str) -> bool -> Option A
Quick Check:
Immutable borrow + &str + bool return [OK]
Hint: Search uses &self and returns bool [OK]
Common Mistakes:
Using mutable borrow &mut self unnecessarily
Returning wrong type like String or usize
Taking owned String instead of &str
3. Given the following Rust Trie search code snippet, what will be the output when searching for "cat"?
let mut trie = Trie::new();
trie.insert("car");
trie.insert("cat");
trie.insert("cap");
println!("{}", trie.search("cat"));
medium
A. Compilation error
B. false
C. panic at runtime
D. true
Solution
Step 1: Analyze inserted words
The words "car", "cat", and "cap" are inserted into the Trie, so "cat" is present.
Step 2: Check search output for "cat"
Searching "cat" should return true because it was inserted.
Final Answer:
true -> Option D
Quick Check:
Inserted words include "cat" so search returns true [OK]
Hint: Search returns true if word was inserted [OK]
Common Mistakes:
Assuming search returns false if similar words exist
Confusing insert and search methods
Expecting runtime errors without reason
4. Identify the bug in this Rust Trie search method snippet:
fn search(&self, word: &str) -> bool {
let mut node = &self.root;
for ch in word.chars() {
if !node.children.contains_key(&ch) {
return false;
}
node = node.children.get(&ch).unwrap();
}
true
}
medium
A. Uses unwrap() which causes panic always
B. Returns true even if word is a prefix but not complete word
C. Does not iterate over characters correctly
D. Incorrect return type
Solution
Step 1: Understand search logic
The code checks if each character exists in children nodes, moving down the Trie.
Step 2: Check final return condition
It returns true after loop without verifying if current node marks end of a complete word.
Step 3: Identify bug
This causes search to return true for prefixes that are not full words.
Final Answer:
Returns true even if word is a prefix but not complete word -> Option B
5. You want to modify a Trie search operation in Rust to support searching with wildcard character '.' which can match any single letter. Which approach is best to implement this feature?
hard
A. Use recursion to explore all children nodes when '.' is encountered
B. Replace '.' with all alphabets before searching iteratively
C. Ignore '.' and treat it as a normal character
D. Use a hash map to store wildcard positions and skip them
Solution
Step 1: Understand wildcard '.' meaning
The '.' can match any single character, so search must consider all possible children at that position.
Step 2: Choose search strategy
Recursion allows exploring all children nodes when '.' is found, checking all paths.
Step 3: Evaluate other options
Replacing '.' with all alphabets is inefficient; ignoring '.' is incorrect; hash map for positions doesn't solve matching.
Final Answer:
Use recursion to explore all children nodes when '.' is encountered -> Option A
Quick Check:
Recursion handles wildcards by branching [OK]
Hint: Use recursion to handle '.' wildcard in search [OK]
Common Mistakes:
Ignoring wildcard meaning
Trying to replace '.' with all letters before search
Skipping recursive exploration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.