Challenge - 5 Problems
HashMap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Basic HashMap Insertions
What is the printed state of the hash map after inserting keys 1, 2, and 3 with values 'A', 'B', and 'C' respectively?
DSA C
typedef struct Node {
int key;
char value;
struct Node* next;
} Node;
#define SIZE 5
Node* hashMap[SIZE] = {NULL};
int hash(int key) {
return key % SIZE;
}
void insert(int key, char value) {
int index = hash(key);
Node* newNode = malloc(sizeof(Node));
newNode->key = key;
newNode->value = value;
newNode->next = hashMap[index];
hashMap[index] = newNode;
}
void printHashMap() {
for (int i = 0; i < SIZE; i++) {
printf("Bucket %d: ", i);
Node* temp = hashMap[i];
while (temp) {
printf("(%d -> %c) -> ", temp->key, temp->value);
temp = temp->next;
}
printf("NULL\n");
}
}
int main() {
insert(1, 'A');
insert(2, 'B');
insert(3, 'C');
printHashMap();
return 0;
}Attempts:
2 left
💡 Hint
Remember the hash function is key % SIZE, and SIZE is 5.
✗ Incorrect
The hash function places keys 1, 2, and 3 into buckets 1, 2, and 3 respectively. Each bucket contains one node with the inserted key and value.
❓ Predict Output
intermediate2:00remaining
Collision Handling in HashMap
What is the printed state of the hash map after inserting keys 1, 6, and 11 with values 'X', 'Y', and 'Z' respectively?
DSA C
typedef struct Node {
int key;
char value;
struct Node* next;
} Node;
#define SIZE 5
Node* hashMap[SIZE] = {NULL};
int hash(int key) {
return key % SIZE;
}
void insert(int key, char value) {
int index = hash(key);
Node* newNode = malloc(sizeof(Node));
newNode->key = key;
newNode->value = value;
newNode->next = hashMap[index];
hashMap[index] = newNode;
}
void printHashMap() {
for (int i = 0; i < SIZE; i++) {
printf("Bucket %d: ", i);
Node* temp = hashMap[i];
while (temp) {
printf("(%d -> %c) -> ", temp->key, temp->value);
temp = temp->next;
}
printf("NULL\n");
}
}
int main() {
insert(1, 'X');
insert(6, 'Y');
insert(11, 'Z');
printHashMap();
return 0;
}Attempts:
2 left
💡 Hint
All keys hash to the same bucket because of the modulo operation.
✗ Incorrect
Keys 1, 6, and 11 all hash to bucket 1 (1 % 5 = 1, 6 % 5 = 1, 11 % 5 = 1). New nodes are inserted at the head, so the last inserted key appears first.
🔧 Debug
advanced2:00remaining
Find the Bug in HashMap Search Function
What error will occur when running this search function in the hash map implementation?
DSA C
char search(int key) { int index = hash(key); Node* temp = hashMap[index]; while (temp != NULL) { if (temp->key == key) { return temp->value; } temp = temp->next; } return '\0'; }
Attempts:
2 left
💡 Hint
Check the if condition inside the while loop carefully.
✗ Incorrect
The if condition uses assignment (=) instead of comparison (==). This causes the condition to always be true (except when key is 0), leading to incorrect behavior or infinite loop.
❓ Predict Output
advanced2:00remaining
Output After Deleting a Key from HashMap
What is the printed state of the hash map after inserting keys 2, 7, 12 with values 'P', 'Q', 'R' and then deleting key 7?
DSA C
typedef struct Node {
int key;
char value;
struct Node* next;
} Node;
#define SIZE 5
Node* hashMap[SIZE] = {NULL};
int hash(int key) {
return key % SIZE;
}
void insert(int key, char value) {
int index = hash(key);
Node* newNode = malloc(sizeof(Node));
newNode->key = key;
newNode->value = value;
newNode->next = hashMap[index];
hashMap[index] = newNode;
}
void delete(int key) {
int index = hash(key);
Node* temp = hashMap[index];
Node* prev = NULL;
while (temp) {
if (temp->key == key) {
if (prev == NULL) {
hashMap[index] = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
return;
}
prev = temp;
temp = temp->next;
}
}
void printHashMap() {
for (int i = 0; i < SIZE; i++) {
printf("Bucket %d: ", i);
Node* temp = hashMap[i];
while (temp) {
printf("(%d -> %c) -> ", temp->key, temp->value);
temp = temp->next;
}
printf("NULL\n");
}
}
int main() {
insert(2, 'P');
insert(7, 'Q');
insert(12, 'R');
delete(7);
printHashMap();
return 0;
}Attempts:
2 left
💡 Hint
Remember the hash function and that new nodes are inserted at the head.
✗ Incorrect
Keys 2, 7, and 12 all hash to bucket 2. Insertions add nodes at the head, so order before deletion is 12 -> 7 -> 2. Deleting key 7 removes the middle node, leaving 12 -> 2.
🧠 Conceptual
expert2:00remaining
Why Use Linked Lists for Collision Handling in HashMap?
Which is the main reason linked lists are used to handle collisions in a hash map implementation?
Attempts:
2 left
💡 Hint
Think about what happens when two keys hash to the same bucket.
✗ Incorrect
Linked lists allow multiple nodes to be chained in the same bucket, handling collisions by storing all collided key-value pairs without a fixed size limit.
