Bird
0
0
DSA Cprogramming~20 mins

HashMap Implementation from Scratch in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HashMap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A
Bucket 0: NULL
Bucket 1: (1 -&gt; A) -&gt; NULL
Bucket 2: (2 -&gt; B) -&gt; NULL
Bucket 3: (3 -&gt; C) -&gt; NULL
Bucket 4: NULL
B
Bucket 0: NULL
Bucket 1: (1 -&gt; A) -&gt; NULL
Bucket 2: NULL
Bucket 3: (3 -&gt; C) -&gt; NULL
Bucket 4: (2 -&gt; B) -&gt; NULL
C
Bucket 0: NULL
Bucket 1: (1 -&gt; A) -&gt; NULL
Bucket 2: (2 -&gt; B) -&gt; NULL
Bucket 3: NULL
Bucket 4: (3 -&gt; C) -&gt; NULL
D
Bucket 0: NULL
Bucket 1: NULL
Bucket 2: (2 -&gt; B) -&gt; NULL
Bucket 3: (3 -&gt; C) -&gt; NULL
Bucket 4: (1 -&gt; A) -&gt; NULL
Attempts:
2 left
💡 Hint
Remember the hash function is key % SIZE, and SIZE is 5.
Predict Output
intermediate
2: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;
}
A
Bucket 0: NULL
Bucket 1: (11 -&gt; Z) -&gt; (6 -&gt; Y) -&gt; (1 -&gt; X) -&gt; NULL
Bucket 2: NULL
Bucket 3: NULL
Bucket 4: NULL
B
Bucket 0: NULL
Bucket 1: (1 -&gt; X) -&gt; NULL
Bucket 2: (6 -&gt; Y) -&gt; NULL
Bucket 3: (11 -&gt; Z) -&gt; NULL
Bucket 4: NULL
C
Bucket 0: NULL
Bucket 1: (6 -&gt; Y) -&gt; (11 -&gt; Z) -&gt; (1 -&gt; X) -&gt; NULL
Bucket 2: NULL
Bucket 3: NULL
Bucket 4: NULL
D
Bucket 0: NULL
Bucket 1: (1 -&gt; X) -&gt; (6 -&gt; Y) -&gt; (11 -&gt; Z) -&gt; NULL
Bucket 2: NULL
Bucket 3: NULL
Bucket 4: NULL
Attempts:
2 left
💡 Hint
All keys hash to the same bucket because of the modulo operation.
🔧 Debug
advanced
2: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';
}
ARuntime crash due to null pointer dereference
BCompilation error due to invalid pointer usage
CNo error, returns the correct value if key found
DInfinite loop due to assignment in if condition
Attempts:
2 left
💡 Hint
Check the if condition inside the while loop carefully.
Predict Output
advanced
2: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;
}
A
Bucket 0: NULL
Bucket 1: NULL
Bucket 2: (7 -&gt; Q) -&gt; (12 -&gt; R) -&gt; (2 -&gt; P) -&gt; NULL
Bucket 3: NULL
Bucket 4: NULL
B
Bucket 0: NULL
Bucket 1: NULL
Bucket 2: (2 -&gt; P) -&gt; (12 -&gt; R) -&gt; NULL
Bucket 3: NULL
Bucket 4: NULL
C
Bucket 0: NULL
Bucket 1: NULL
Bucket 2: (12 -&gt; R) -&gt; (2 -&gt; P) -&gt; NULL
Bucket 3: NULL
Bucket 4: NULL
D
Bucket 0: NULL
Bucket 1: NULL
Bucket 2: (12 -&gt; R) -&gt; NULL
Bucket 3: NULL
Bucket 4: NULL
Attempts:
2 left
💡 Hint
Remember the hash function and that new nodes are inserted at the head.
🧠 Conceptual
expert
2: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?
ALinked lists improve the hash function's speed by sorting keys automatically.
BLinked lists allow storing multiple key-value pairs in the same bucket efficiently without fixed size limits.
CLinked lists prevent collisions by changing the hash function dynamically.
DLinked lists reduce memory usage by storing keys as indexes instead of values.
Attempts:
2 left
💡 Hint
Think about what happens when two keys hash to the same bucket.