Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new empty hash map in Rust.
DSA Rust
let mut map = std::collections::HashMap::[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' instead of 'new' to create the map.
Trying to remove or clear before creating the map.
✗ Incorrect
The method 'new' creates an empty HashMap in Rust.
2fill in blank
mediumComplete the code to insert a key-value pair into the hash map.
DSA Rust
map.[1]("apple", 3);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which only reads values.
Using 'remove' which deletes entries.
✗ Incorrect
The 'insert' method adds a key-value pair to the hash map.
3fill in blank
hardFix the error in the code to check if a key exists in the hash map.
DSA Rust
if map.[1]("banana") { println!("Found banana"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' which adds keys instead of checking.
Using 'get_mut' which returns a mutable reference.
✗ Incorrect
The 'contains_key' method returns true if the key exists in the map.
4fill in blank
hardFill both blanks to compute a simple hash value for a string key.
DSA Rust
fn simple_hash(key: &str) -> usize {
key.bytes().[1](0, |acc, b| acc [2] b as usize)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'fold' which does not reduce.
Using '*' which multiplies and can overflow easily.
✗ Incorrect
The 'fold' method accumulates values, and '+' sums the bytes for a simple hash.
5fill in blank
hardFill all three blanks to create a hash map from a list of keys with their lengths as values.
DSA Rust
let keys = vec!["cat", "dog", "bird"]; let map: std::collections::HashMap<_, _> = keys.into_iter().map(|[1]| ([2], [3])).collect();
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' which is undefined here.
Using 'key' inconsistently in the tuple.
✗ Incorrect
We name the iterator variable 'key', then map it to a tuple of (key, key.len()) to store lengths.
