Challenge - 5 Problems
HashMap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of put and get operations in custom HashMap
What is the output of the following Java code using a custom HashMap implementation after inserting and retrieving values?
DSA Java
CustomHashMap<String, Integer> map = new CustomHashMap<>(); map.put("apple", 10); map.put("banana", 20); map.put("apple", 30); System.out.println(map.get("apple")); System.out.println(map.get("banana")); System.out.println(map.get("cherry"));
Attempts:
2 left
💡 Hint
Remember that putting a value with an existing key updates the value.
✗ Incorrect
The key "apple" is first inserted with 10, then updated to 30. "banana" is 20. "cherry" is not present, so get returns null.
🧠 Conceptual
intermediateUnderstanding collision handling in custom HashMap
In a custom HashMap implementation using separate chaining, what happens when two different keys produce the same hash index?
Attempts:
2 left
💡 Hint
Think about how separate chaining works to handle collisions.
✗ Incorrect
Separate chaining stores multiple entries in a linked list at the same bucket index to handle collisions.
🔧 Debug
advancedIdentify the bug in put method of custom HashMap
What is the bug in this put method snippet of a custom HashMap that uses separate chaining?
DSA Java
public void put(K key, V value) {
int index = key.hashCode() % buckets.length;
Node<K, V> head = buckets[index];
while (head != null) {
if (head.key.equals(key)) {
head.value = value;
return;
}
head = head.next;
}
Node<K, V> newNode = new Node<>(key, value);
newNode.next = buckets[index];
buckets[index] = newNode;
}Attempts:
2 left
💡 Hint
Consider what happens if hashCode returns a negative number.
✗ Incorrect
Using % operator directly can produce negative indices if hashCode is negative, causing ArrayIndexOutOfBoundsException.
📝 Syntax
advancedCorrect syntax for generic Node class in custom HashMap
Which option correctly declares a generic Node class for a custom HashMap in Java?
Attempts:
2 left
💡 Hint
The next node should have the same generic types as the current node.
✗ Incorrect
Option A correctly uses generics for key, value, and next node. Others miss generics or mismatch types.
🚀 Application
expertResulting size after multiple put and remove operations
Given a custom HashMap with separate chaining, what is the size of the map after these operations?
DSA Java
CustomHashMap<String, Integer> map = new CustomHashMap<>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); map.put("a", 4); map.remove("b"); map.put("d", 5); map.remove("e");
Attempts:
2 left
💡 Hint
Remember that updating a key does not increase size and removing a non-existent key does nothing.
✗ Incorrect
Keys "a", "c", and "d" remain. "b" is removed. "e" was never added, so size is 3.
