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 is stored without searching the whole structure.
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 correctly.
Click to reveal answer
intermediate
Name one common method to handle collisions in a HashMap.
One common method is chaining, where each index in the array holds a linked list of key-value pairs that share the same index.
Click to reveal answer
beginner
What is the average time complexity for searching a value in a well-implemented HashMap?
The average time complexity is O(1), meaning it takes constant time to find a value using its key.
Click to reveal answer
What does a hash function do in a HashMap?
AConverts a key into an index
BSorts the keys alphabetically
CStores values in a linked list
DDeletes duplicate keys
✗ Incorrect
A hash function converts a key into an index to store or find the value quickly.
What is a collision in a HashMap?
AWhen the HashMap is empty
BWhen two keys have the same value
CWhen a key is not found
DWhen two keys produce the same index
✗ Incorrect
A collision happens when two different keys produce the same index from the hash function.
Which method can handle collisions in a HashMap?
AChaining with linked lists
BBinary search
CSorting keys
DUsing arrays only
✗ Incorrect
Chaining uses linked lists at each index to store multiple key-value pairs that collide.
What is the average time complexity to find a value in a HashMap?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
A well-implemented HashMap allows constant time O(1) lookup on average.
In a HashMap, what data structure is commonly used to store multiple values at the same index due to collisions?
AArray
BLinked list
CStack
DQueue
✗ Incorrect
Linked lists are commonly used to store multiple key-value pairs at the same index in chaining.
Explain how a HashMap stores and retrieves data using a hash function and handles collisions.
Think about how keys become positions and what happens if two keys want the same spot.
You got /5 concepts.
Describe the advantages of using a HashMap compared to a simple list for storing key-value pairs.
Focus on speed and efficiency when finding values.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a hash function in a HashMap implementation?
easy
A. To sort the keys in ascending order
B. To store values in a linked list
C. To convert a key into an index for storing the value
D. To delete keys from the HashMap
Solution
Step 1: Understand the role of a hash function
A hash function takes a key and converts it into a number (index) that tells where to store the value in the HashMap's internal array.
Step 2: Compare other options
Sorting keys or deleting keys are not the job of the hash function. Storing values in a linked list is a collision handling method, not hashing.
Final Answer:
To convert a key into an index for storing the value -> Option C
Quick Check:
Hash function = key to index [OK]
Hint: Hash function maps keys to array indexes fast [OK]
Common Mistakes:
Thinking hash function sorts keys
Confusing hash function with collision handling
Believing hash function deletes keys
2. Which of the following is the correct way to declare a simple HashMap structure in C that stores integer keys and values?
easy
A. typedef struct { int key; int value; } HashNode;
B. typedef struct { char* key; int value; } HashMap;
C. typedef struct { int key; int value; } HashMap;
D. typedef struct { int key; int value; HashNode* next; } HashMap;
Solution
Step 1: Identify the basic node structure
A HashMap stores key-value pairs. A node with int key and int value is a basic unit, so HashNode struct is correct.
Step 2: Check other options
typedef struct { char* key; int value; } HashMap; uses char* key which is not integer keys. typedef struct { int key; int value; } HashMap; names the struct HashMap but it's actually a node. typedef struct { int key; int value; HashNode* next; } HashMap; adds a pointer for chaining but names it HashMap, which is confusing.
Final Answer:
typedef struct { int key; int value; } HashNode; -> Option A
Quick Check:
Node struct with int key and value = typedef struct { int key; int value; } HashNode; [OK]
Hint: Node struct holds key and value pairs [OK]
Common Mistakes:
Confusing node and map struct names
Using wrong key types
Missing pointer for chaining in basic struct
3. Given the following code snippet for inserting keys into a HashMap with size 5, what will be the index for key 12?
int hash(int key) {
return key % 5;
}
int index = hash(12);
printf("%d", index);
medium
A. 4
B. 2
C. 3
D. 1
Solution
Step 1: Apply the hash function to key 12
The hash function returns key % 5. For key 12, 12 % 5 = 2.
Step 2: Confirm the printed index
The code prints the index, which is 2.
Final Answer:
2 -> Option B
Quick Check:
12 % 5 = 2 [OK]
Hint: Modulo operator gives index in hash function [OK]
Common Mistakes:
Calculating 12 % 5 incorrectly
Confusing modulo with division
Ignoring hash function logic
4. Consider this buggy code snippet for inserting a key-value pair into a HashMap with chaining:
void insert(HashNode** table, int size, int key, int value) {
int index = key % size;
HashNode* newNode = malloc(sizeof(HashNode));
newNode->key = key;
newNode->value = value;
newNode->next = NULL;
if (table[index] == NULL) {
table[index] = newNode;
} else {
table[index]->next = newNode;
}
}
What is the main problem in this code?
medium
A. It overwrites the existing chain instead of appending correctly
B. It does not handle memory allocation failure
C. It uses wrong hash function
D. It does not set the key and value fields
Solution
Step 1: Analyze insertion logic for chaining
If the bucket is empty, it sets the new node. Otherwise, it sets table[index]->next = newNode, which overwrites the existing next pointer, losing the chain.
Step 2: Identify correct chaining method
To append, it should traverse to the last node in the chain and then add the new node, not overwrite the first node's next pointer.
Final Answer:
It overwrites the existing chain instead of appending correctly -> Option A
Quick Check:
Chaining requires appending, not overwriting [OK]
Hint: Traverse chain to append new node, don't overwrite next [OK]
Common Mistakes:
Ignoring chain traversal
Assuming single node per bucket
Not checking for NULL before insertion
5. You want to implement a HashMap from scratch in C that supports integer keys and values, handles collisions using chaining, and dynamically resizes when load factor exceeds 0.7. Which of the following steps is NOT necessary for this implementation?
hard
A. Resize the internal array and rehash all keys when load factor is high
B. Create linked lists at each bucket to handle collisions
C. Implement a hash function to convert keys to indexes
D. Sort the keys in each bucket to speed up search
Solution
Step 1: Understand necessary components of HashMap
A hash function, chaining with linked lists, and resizing with rehashing are all essential for correct and efficient HashMap behavior.
Step 2: Evaluate sorting keys in buckets
Sorting keys in buckets is not required because chaining uses linked lists and search is done by traversing nodes. Sorting adds unnecessary overhead.
Final Answer:
Sort the keys in each bucket to speed up search -> Option D
Quick Check:
Sorting buckets is unnecessary in chaining [OK]
Hint: Chaining uses linked lists; sorting buckets is extra work [OK]
Common Mistakes:
Thinking sorting improves chaining search
Ignoring rehashing on resize
Skipping collision handling
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.