Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new empty hash map for counting frequencies.
DSA Rust
let mut freq_map = std::collections::HashMap::[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using insert instead of new to create the map.
Trying to use clear on a map that doesn't exist yet.
✗ Incorrect
We use HashMap::new() to create an empty hash map for frequency counting.
2fill in blank
mediumComplete the code to increase the count of a character in the frequency map.
DSA Rust
let count = freq_map.entry(ch).or_insert([1]); *count += 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Starting count at 1 instead of 0 causes off-by-one errors.
Using the character itself as the count.
✗ Incorrect
We start counting from zero, so or_insert(0) sets initial count to zero.
3fill in blank
hardFix the error in the code to check if two strings have the same character frequencies.
DSA Rust
if freq_map1 == [1] { println!("Strings have the same frequency"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing freq_map1 with itself instead of freq_map2.
Using an undefined variable for comparison.
✗ Incorrect
We compare freq_map1 with freq_map2 to check if frequencies match.
4fill in blank
hardFill both blanks to count frequencies of characters in a string.
DSA Rust
for ch in [1].chars() { let count = freq_map.entry(ch).or_insert([2]); *count += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'chars' as the string variable instead of calling chars() method.
Starting counts at 1 instead of 0.
✗ Incorrect
We iterate over input_string characters and start counts at zero.
5fill in blank
hardFill all three blanks to create a frequency map with characters as keys and counts as values.
DSA Rust
let freq_map: std::collections::HashMap<[1], [2]> = input_string.chars().fold(std::collections::HashMap::new(), |mut acc, ch| { *acc.entry(ch).or_insert([3]) += 1; acc });
Attempts:
3 left
💡 Hint
Common Mistakes
Using String instead of char for keys.
Starting counts at 1 instead of 0.
✗ Incorrect
Keys are chars, values are i32 counts starting at zero.
