Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new empty HashMap for counting characters.
DSA Rust
let mut freq = std::collections::HashMap::[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using insert or get instead of new to create the map.
Forgetting to make the map mutable.
✗ Incorrect
We use HashMap::new() to create an empty map to store character counts.
2fill in blank
mediumComplete the code to get the count of a character or default to 0 if not found.
DSA Rust
let count = *freq.get(&ch).unwrap_or(&[1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as default count which is incorrect.
Using None which is not a number.
✗ Incorrect
unwrap_or(&0) returns 0 if the character is not yet in the map.
3fill in blank
hardFix the error in updating the count for the character in the map.
DSA Rust
freq.insert(ch, count [1] 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to update the map after increment.
✗ Incorrect
We add 1 to the current count to update the frequency.
4fill in blank
hardFill both blanks to complete the loop that counts characters in the string.
DSA Rust
for [1] in text.chars() { let count = *freq.get(&[2]).unwrap_or(&0); freq.insert([2], count + 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inside and outside the loop.
Using the whole string 'text' instead of a character.
✗ Incorrect
We use 'ch' as the variable for each character in the loop and to update the map.
5fill in blank
hardFill all three blanks to create a HashMap of character frequencies using iterator methods.
DSA Rust
let freq: std::collections::HashMap<char, usize> = text.chars()
.fold(std::collections::HashMap::[1](), |mut acc, [2]| {
*acc.entry([3]).or_insert(0) += 1;
acc
});Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names inconsistent with chars().
Not using HashMap::new() to start fold.
✗ Incorrect
We create a new HashMap, use 'c' as the character variable, and update counts with entry().or_insert(0).
