Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a frequency map from a string.
DSA Rust
let mut freq_map = std::collections::HashMap::new(); for ch in text.chars() { *freq_map.entry(ch).or_insert([1]) += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 causes counts to start at 2.
Using None causes a type error.
✗ Incorrect
We start counting frequencies from zero, so or_insert(0) initializes the count.
2fill in blank
mediumComplete the code to pop the smallest frequency node from a binary heap.
DSA Rust
let node = heap.pop().[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap_err() causes panic because pop() returns Some or None, not an error.
Using expect() requires a message string.
✗ Incorrect
pop() returns an Option, unwrap() extracts the value safely if present.
3fill in blank
hardFix the error in the code to insert a new node into the heap.
DSA Rust
heap.[1](new_node);Attempts:
3 left
💡 Hint
Common Mistakes
Using insert() causes a compile error.
Using append() is for vectors, not heaps.
✗ Incorrect
BinaryHeap uses push() to add elements, insert() or add() do not exist.
4fill in blank
hardFill both blanks to build a recursive function that assigns codes to characters.
DSA Rust
fn assign_codes(node: &Node, prefix: String, codes: &mut std::collections::HashMap<char, String>) {
if node.left.is_none() && node.right.is_none() {
codes.insert(node.ch.unwrap(), prefix);
} else {
if let Some(ref left) = node.left {
assign_codes(left, prefix.clone() + [1], codes);
}
if let Some(ref right) = node.right {
assign_codes(right, prefix.clone() + [2], codes);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using empty strings causes all codes to be the same.
Using spaces causes incorrect codes.
✗ Incorrect
Left edges add '0' and right edges add '1' to the prefix code.
5fill in blank
hardFill all three blanks to create a frequency map, build a heap, and pop the smallest node.
DSA Rust
let mut freq_map = std::collections::HashMap::new(); for ch in text.chars() { *freq_map.entry(ch).or_insert([1]) += 1; } let mut heap = std::collections::BinaryHeap::new(); for (ch, freq) in freq_map { heap.[2](Node::new_leaf(ch, freq)); } let smallest = heap.pop().[3]();
Attempts:
3 left
💡 Hint
Common Mistakes
Starting counts at 1 causes off-by-one errors.
Using insert instead of push causes errors.
Not unwrapping pop() causes type errors.
✗ Incorrect
Initialize counts at 0, push nodes into heap, and unwrap popped node safely.
