Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a character into the Trie node's children.
DSA Rust
node.children.insert([1], TrieNode::new());Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a character literal.
Inserting a value instead of a key.
✗ Incorrect
We insert the character 'c' as a key in the children map to create a new TrieNode.
2fill in blank
mediumComplete the code to check if the Trie node has a child for the given character.
DSA Rust
if node.children.contains_key(&[1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name.
Not using a reference (&) when checking the key.
✗ Incorrect
We check if the children map contains the character 'ch' as a key.
3fill in blank
hardFix the error in the code to mark the end of a word in the Trie.
DSA Rust
node.is_end_of_word = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting the flag to false or zero.
Using None instead of a boolean.
✗ Incorrect
We set is_end_of_word to true to mark the end of a valid word in the Trie.
4fill in blank
hardFill both blanks to complete the loop that traverses the Trie for each character in the word.
DSA Rust
for [1] in word.chars() { if !node.children.contains_key(&[2]) { return false; } node = node.children.get_mut(&ch).unwrap(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for loop and key check.
Using incorrect variable names.
✗ Incorrect
We use 'ch' as the loop variable and check if 'ch' exists in children keys.
5fill in blank
hardFill all three blanks to complete the function that inserts a word into the Trie.
DSA Rust
fn insert(&mut self, word: &str) {
let mut node = &mut self.root;
for [1] in word.chars() {
node = node.children.entry([2]).or_insert(TrieNode::new());
}
node.[3] = true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for loop variable and key.
Forgetting to mark the end of the word.
✗ Incorrect
We use 'ch' as the loop variable and key, and set is_end_of_word to true at the end.
