Bird
0
0
DSA Cprogramming~20 mins

Collision Handling Using Chaining in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Chaining Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of inserting keys in a chained hash table
What is the printed state of the hash table after inserting keys 10, 22, 31, 4, 15, 28, 17, 88, 59 using chaining with hash function h(key) = key % 10?
DSA C
typedef struct Node {
    int key;
    struct Node* next;
} Node;

Node* hashTable[10] = {NULL};

void insert(int key) {
    int index = key % 10;
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->key = key;
    newNode->next = hashTable[index];
    hashTable[index] = newNode;
}

void printTable() {
    for (int i = 0; i < 10; i++) {
        printf("%d: ", i);
        Node* temp = hashTable[i];
        while (temp) {
            printf("%d -> ", temp->key);
            temp = temp->next;
        }
        printf("NULL\n");
    }
}

int main() {
    int keys[] = {10, 22, 31, 4, 15, 28, 17, 88, 59};
    int n = sizeof(keys)/sizeof(keys[0]);
    for (int i = 0; i < n; i++) {
        insert(keys[i]);
    }
    printTable();
    return 0;
}
A
0: 10 -&gt; NULL
1: 31 -&gt; 41 -&gt; NULL
2: 22 -&gt; NULL
3: NULL
4: 4 -&gt; NULL
5: 15 -&gt; NULL
6: NULL
7: 17 -&gt; NULL
8: 88 -&gt; 28 -&gt; NULL
9: 59 -&gt; NULL
B
0: 10 -&gt; NULL
1: 31 -&gt; NULL
2: 22 -&gt; NULL
3: NULL
4: 4 -&gt; NULL
5: 15 -&gt; NULL
6: NULL
7: 17 -&gt; NULL
8: 28 -&gt; 88 -&gt; NULL
9: 59 -&gt; NULL
C
0: 10 -&gt; NULL
1: 31 -&gt; NULL
2: 22 -&gt; NULL
3: NULL
4: 4 -&gt; NULL
5: 15 -&gt; NULL
6: NULL
7: 17 -&gt; NULL
8: 88 -&gt; 28 -&gt; NULL
9: 59 -&gt; NULL
D
0: 10 -&gt; NULL
1: 31 -&gt; NULL
2: 22 -&gt; NULL
3: NULL
4: 4 -&gt; NULL
5: 15 -&gt; NULL
6: NULL
7: 17 -&gt; NULL
8: 88 -&gt; NULL
9: 59 -&gt; NULL
Attempts:
2 left
💡 Hint
Remember that new nodes are inserted at the head of the linked list for each bucket.
🧠 Conceptual
intermediate
1:00remaining
Number of items in a chained hash table bucket
If a hash table uses chaining and the hash function distributes keys uniformly, what is the expected number of items in each bucket after inserting n keys into a table with m buckets?
Am / n
Bn / m
Cn * m
Dn + m
Attempts:
2 left
💡 Hint
Think about how keys are spread evenly across buckets.
🔧 Debug
advanced
1:30remaining
Identify the bug in chaining insertion code
What error will this code cause when inserting keys into a chained hash table? Code snippet: void insert(int key) { int index = key % 10; Node* newNode = (Node*)malloc(sizeof(Node)); newNode->key = key; newNode->next = NULL; if (hashTable[index] == NULL) { hashTable[index] = newNode; } else { Node* temp = hashTable[index]; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } Assume hashTable is an array of Node* initialized to NULL.
ANo error; code inserts at the end of the chain correctly.
BMemory leak due to not freeing nodes.
CSegmentation fault due to uninitialized hashTable array.
DInfinite loop in while condition.
Attempts:
2 left
💡 Hint
Check how the linked list is traversed and where the new node is added.
Predict Output
advanced
2:00remaining
Output after deleting a key from chained hash table
Given the hash table with chaining below, what is the printed state after deleting key 15? Initial state: 0: 10 -> NULL 1: 31 -> NULL 2: 22 -> NULL 3: NULL 4: 4 -> NULL 5: 15 -> 25 -> NULL 6: NULL 7: 17 -> NULL 8: 88 -> 28 -> NULL 9: 59 -> NULL Deletion code removes the first occurrence of key 15 in the chain at index 5.
DSA C
void deleteKey(int key) {
    int index = key % 10;
    Node* temp = hashTable[index];
    Node* prev = NULL;
    while (temp != NULL && temp->key != key) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return; // key not found
    if (prev == NULL) {
        hashTable[index] = temp->next;
    } else {
        prev->next = temp->next;
    }
    free(temp);
}

// After calling deleteKey(15), printTable() is called.
A5: 15 -> 25 -> NULL
B5: NULL
C5: 25 -> 15 -> NULL
D5: 25 -> NULL
Attempts:
2 left
💡 Hint
Deleting key 15 removes the first node with key 15 in the chain.
🧠 Conceptual
expert
1:30remaining
Why chaining is preferred over open addressing in high load factor scenarios
Why is chaining often preferred over open addressing when the hash table load factor is high (close to or greater than 1)?
AChaining allows unlimited number of elements per bucket, avoiding clustering and performance degradation.
BOpen addressing uses linked lists internally, which are slower than arrays.
CChaining requires less memory than open addressing at high load factors.
DOpen addressing cannot handle collisions at all.
Attempts:
2 left
💡 Hint
Think about how collisions are handled and how performance changes as table fills up.