Challenge - 5 Problems
HashMap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust HashMap insertion code?
Consider a simple HashMap implemented with separate chaining. After inserting keys 1, 2, and 3 with values 'a', 'b', and 'c', what is the printed state of the map?
DSA Rust
let mut map = std::collections::HashMap::new(); map.insert(1, 'a'); map.insert(2, 'b'); map.insert(3, 'c'); println!("{:?}", map);
Attempts:
2 left
💡 Hint
Think about how insertion order affects the printed output in Rust's HashMap debug format.
✗ Incorrect
Rust's HashMap does not guarantee order, but the debug print shows all inserted key-value pairs. Since all keys 1, 2, and 3 are inserted, all appear in the output. Option C shows all pairs correctly.
🧠 Conceptual
intermediateWhich collision resolution method is used in this HashMap implementation?
In a HashMap implemented from scratch using an array of buckets where each bucket holds a linked list of entries, what collision resolution technique is being used?
Attempts:
2 left
💡 Hint
Think about how collisions are handled when multiple keys hash to the same bucket.
✗ Incorrect
Separate chaining uses linked lists (or other structures) in each bucket to store multiple entries that hash to the same index. This matches the description.
🔧 Debug
advancedWhat error does this Rust HashMap get method code produce?
Given this code snippet trying to get a value from a custom HashMap, what error occurs?
DSA Rust
let mut map = std::collections::HashMap::new(); map.insert(1, "one"); let value = map.get(&2).unwrap(); println!("{}", value);
Attempts:
2 left
💡 Hint
What happens when you unwrap an Option that is None?
✗ Incorrect
Calling unwrap on None causes a panic at runtime. Since key 2 was not inserted, get returns None, so unwrap panics.
🚀 Application
advancedHow many items are in the HashMap after these operations?
Starting with an empty HashMap, perform these operations: insert key 1 with value 'x', insert key 2 with value 'y', insert key 1 with value 'z', then remove key 2. How many items remain?
DSA Rust
let mut map = std::collections::HashMap::new(); map.insert(1, 'x'); map.insert(2, 'y'); map.insert(1, 'z'); map.remove(&2); println!("{}", map.len());
Attempts:
2 left
💡 Hint
Remember that inserting a key that already exists updates its value.
✗ Incorrect
Inserting key 1 twice updates its value, so only one entry for key 1 exists. Key 2 is removed, so only one item remains.
❓ Predict Output
expertWhat is the output of this Rust HashMap custom hash function code?
Given a custom HashMap implementation with a simple hash function that returns key modulo 3, and inserting keys 3, 6, 9 with values 'a', 'b', 'c', what is the final state of the buckets?
DSA Rust
struct Entry {
key: i32,
value: char,
next: Option<Box<Entry>>,
}
let mut buckets: Vec<Option<Box<Entry>>> = vec![None; 3];
fn hash(key: i32) -> usize {
(key % 3) as usize
}
fn insert(buckets: &mut Vec<Option<Box<Entry>>>, key: i32, value: char) {
let index = hash(key);
let new_entry = Box::new(Entry { key, value, next: buckets[index].take() });
buckets[index] = Some(new_entry);
}
insert(&mut buckets, 3, 'a');
insert(&mut buckets, 6, 'b');
insert(&mut buckets, 9, 'c');
// Print bucket keys in order from head to tail for each bucket
for (i, bucket) in buckets.iter().enumerate() {
print!("Bucket {}: ", i);
let mut current = bucket.as_ref();
while let Some(entry) = current {
print!("{} -> ", entry.key);
current = entry.next.as_ref();
}
println!("null");
}Attempts:
2 left
💡 Hint
New entries are added at the head of the linked list in each bucket.
✗ Incorrect
Each key hashes to bucket 0 (3 % 3 = 0). Insertions add new entries at the head, so the last inserted key 9 is at the head, followed by 6, then 3.
