Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a word into the Trie node's children.
DSA Rust
node.children.insert([1], TrieNode::new());Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes instead of single quotes for char keys.
Trying to insert the whole node instead of a character key.
✗ Incorrect
We insert a character key into the children HashMap. It must be a char literal like 'c'.
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 quotes around the variable name, making it a string literal.
Passing the node instead of the character.
✗ Incorrect
The variable ch is a char, so we check contains_key with &ch.
3fill in blank
hardFix the error in this code to get a mutable reference to the child node for character ch.
DSA Rust
let child = node.children.get_mut(&[1]).unwrap();Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string literal instead of a char.
Passing a reference when & is already in code.
✗ Incorrect
We must pass &ch where ch is a char variable. So &ch is correct, but since code already has &, just ch is needed.
4fill in blank
hardFill both blanks to complete the prefix search in the Trie.
DSA Rust
for ch in prefix.chars() { if !node.children.contains_key(&[1]) { return false; } node = node.children.get_mut(&[2]).unwrap(); } return true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using string or char literals instead of the variable.
Using node instead of the character.
✗ Incorrect
We use the variable ch for both contains_key and get_mut calls as the character key.
5fill in blank
hardFill all three blanks to create a HashMap comprehension that maps prefixes to their counts.
DSA Rust
let prefix_counts: HashMap<String, usize> = words.iter()
.flat_map(|word| (1..=word.len()).map(|[1]| word[0..[2]].to_string()))
.fold(HashMap::new(), |mut acc, prefix| {
*acc.entry(prefix).or_insert([3]) += 1;
acc
});Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as initial count which causes double counting on first occurrence.
Using different variable names inconsistently.
✗ Incorrect
We use i as the variable for the range and slice end index, and initialize counts at 0 before incrementing with += 1.
