A HashMap is a data structure that stores key-value pairs. It uses a hash function to compute an index where the value is stored, allowing fast data retrieval.
Click to reveal answer
beginner
Why do we use a hash function in a HashMap?
A hash function converts a key into an index in an array. This helps to quickly find where the value for that key is stored.
Click to reveal answer
intermediate
What is a collision in a HashMap?
A collision happens when two different keys produce the same index from the hash function. We need a way to handle collisions to store both values.
Click to reveal answer
intermediate
How can collisions be handled in a HashMap?
Collisions can be handled by chaining (storing multiple entries in a linked list at the same index) or open addressing (finding another empty slot).
Click to reveal answer
beginner
What is the time complexity of basic operations in a well-implemented HashMap?
In a good HashMap, operations like insert, search, and delete usually take O(1) time on average.
Click to reveal answer
What does a hash function do in a HashMap?
AStores values directly
BSorts the keys alphabetically
CDeletes duplicate keys
DConverts a key into an index
✗ Incorrect
A hash function converts a key into an index where the value is stored.
What is a collision in a HashMap?
AWhen two keys have the same value
BWhen two keys produce the same index
CWhen a key is missing
DWhen the HashMap is empty
✗ Incorrect
A collision happens when two different keys produce the same index.
Which method is NOT used to handle collisions?
ABinary search
BOpen addressing
CChaining
DRehashing
✗ Incorrect
Binary search is not used for collision handling in HashMaps.
What is the average time complexity for searching in a good HashMap?
AO(1)
BO(n)
CO(log n)
DO(n log n)
✗ Incorrect
Searching in a well-implemented HashMap takes O(1) time on average.
In chaining collision handling, what data structure is commonly used to store multiple entries at the same index?
AArray
BStack
CLinked list
DQueue
✗ Incorrect
A linked list is commonly used to store multiple entries in chaining.
Explain how a HashMap stores and retrieves data using a hash function and handles collisions.
Think about how you find a book in a library using a code and what happens if two books have the same code.
You got /4 concepts.
Describe the steps to implement a simple HashMap from scratch.
Imagine building a mailbox system where each mailbox holds letters for certain keys.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a HashMap in Java?
easy
A. To store key-value pairs for fast lookup
B. To store elements in sorted order
C. To store only unique values without keys
D. To store elements in a last-in-first-out order
Solution
Step 1: Understand HashMap functionality
A HashMap stores data as key-value pairs allowing quick access by key.
Step 2: Compare with other data structures
Unlike sorted or stack structures, HashMap focuses on fast lookup, not order or uniqueness of values alone.
Final Answer:
To store key-value pairs for fast lookup -> Option A
Quick Check:
HashMap = key-value fast access [OK]
Hint: HashMap = key-value pairs for quick access [OK]
Common Mistakes:
Confusing HashMap with TreeMap (sorted)
Thinking HashMap stores only unique values
Mixing HashMap with stack or queue behavior
2. Which of the following is the correct way to declare a basic HashMap in Java from scratch?
easy
A. HashMap map = new HashMap<>();
B. HashMap<Integer> map = new HashMap<>();
C. HashMap map = new HashMap();
D. HashMap map = new HashMap();
Solution
Step 1: Check generic syntax for HashMap
Java requires specifying key and value types inside angle brackets <Key, Value>.
Step 2: Identify correct generic declaration
HashMap map = new HashMap(); uses full generic syntax for both declaration and instantiation, which is correct in Java before version 7.
Final Answer:
HashMap map = new HashMap(); -> Option D
Quick Check:
Full generic syntax = HashMap map = new HashMap(); [OK]
Hint: Use on both sides before Java 7 [OK]
Common Mistakes:
Omitting generic types on instantiation (Option A is Java 7+ syntax)
Using only one generic type (Option D is invalid)
Not using generics at all (Option C is raw type)
3. Consider this simplified HashMap put method snippet from scratch:
public void put(K key, V value) {
int index = key.hashCode() % capacity;
if (buckets[index] == null) {
buckets[index] = new Node(key, value);
} else {
Node current = buckets[index];
while (current.next != null) {
if (current.key.equals(key)) {
current.value = value;
return;
}
current = current.next;
}
current.next = new Node(key, value);
}
}
What happens if we insert two keys with the same hashCode but different values?
medium
A. A runtime error occurs due to collision
B. The second key-value pair overwrites the first one
C. Both key-value pairs are stored in a linked list at the same bucket
D. Only the first key-value pair is stored, second is ignored
Solution
Step 1: Understand collision handling in code
If bucket is occupied, code traverses linked nodes to check keys and appends new node if key not found.
Step 2: Analyze insertion of different keys with same hash
Since keys differ but hash is same, new node is added at end of linked list, preserving both pairs.
Final Answer:
Both key-value pairs are stored in a linked list at the same bucket -> Option C
Quick Check:
Collision handled by chaining = Both key-value pairs are stored in a linked list at the same bucket [OK]
Hint: Same hash keys chain in linked list bucket [OK]
Common Mistakes:
Assuming second key overwrites first without key match
Thinking collision causes error
Ignoring linked list chaining for collisions
4. Given this buggy put method snippet from a HashMap implementation:
public void put(K key, V value) {
int index = key.hashCode() % capacity;
if (buckets[index] == null) {
buckets[index] = new Node(key, value);
} else {
Node current = buckets[index];
while (current != null) {
if (current.key.equals(key)) {
current.value = value;
return;
}
current = current.next;
}
current.next = new Node(key, value);
}
}
What is the error in this code?
medium
A. The loop condition should be while (current.next != null) instead of while (current != null)
B. The hashCode modulo operation should use capacity - 1
C. The key comparison should use == instead of equals()
D. The new node is added before checking existing keys
Solution
Step 1: Analyze the while loop condition
The loop runs while current != null, so after loop current becomes null.
Step 2: Check where new node is added
After loop, code tries current.next = new Node(...), but current is null causing NullPointerException.
Step 3: Correct loop condition
Changing loop to while (current.next != null) stops at last node, allowing safe addition at current.next.
Final Answer:
The loop condition should be while (current.next != null) instead of while (current != null) -> Option A
Quick Check:
Loop must stop at last node to add new node [OK]
Hint: Loop to last node, not null, to add new node safely [OK]
Common Mistakes:
Using current != null causes NullPointerException on insertion
Incorrect modulo operation for bucket index
Using == instead of equals() for key comparison
5. You are implementing a HashMap from scratch with an array of buckets and linked lists for collisions. If the initial capacity is 4 and you insert keys with hashCodes: 2, 6, 10, 14, what will be the structure of the buckets after all insertions?
Assume bucket index = hashCode % capacity and each bucket holds a linked list of nodes.
Choose the correct bucket contents representation (keys only):