Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Store all elements that hash to the same index in a linked list at that index
B. Replace the existing element with the new one when a collision occurs
C. Use a secondary hash function to find a new index
D. Resize the hash table immediately when a collision happens

Solution

  1. Step 1: Understand collision in hash tables

    When two keys hash to the same index, a collision occurs.
  2. Step 2: Explain chaining method

    Chaining stores all collided elements in a linked list at that index, allowing multiple elements per slot.
  3. Final Answer:

    Store all elements that hash to the same index in a linked list at that index -> Option A
  4. Quick Check:

    Chaining = linked list at index [OK]
Hint: Chaining means linked list at each hash index [OK]
Common Mistakes:
  • Confusing chaining with open addressing
  • Thinking collisions overwrite existing data
  • Believing resizing happens on every collision
2. Which of the following is the correct way to declare a node structure for chaining in C?
easy
A. typedef struct Node { int key; Node* next; } Node;
B. struct Node { int key; int next; };
C. typedef struct Node { int key; struct Node* next; } Node;
D. struct Node { int key; struct Node next; };

Solution

  1. Step 1: Check correct pointer usage in struct

    In C, to create a linked list node, the next pointer must be a pointer to the same struct type.
  2. Step 2: Validate typedef syntax

    typedef struct Node { int key; struct Node* next; } Node; correctly uses struct Node* next; and typedefs the struct as Node.
  3. Final Answer:

    typedef struct Node { int key; struct Node* next; } Node; -> Option C
  4. Quick Check:

    Pointer to struct Node is struct Node* next [OK]
Hint: Use struct Node* for next pointer in C structs [OK]
Common Mistakes:
  • Using struct Node next instead of pointer
  • Missing typedef keyword or incorrect syntax
  • Declaring next as int instead of pointer
3. Consider the following code snippet for inserting keys into a hash table using chaining. What will be the output after inserting keys 5, 15, and 25 if the hash function is key % 10?
typedef struct Node {
  int key;
  struct Node* next;
} Node;

Node* hashTable[10] = {NULL};

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

// Insert keys
insert(5);
insert(15);
insert(25);

// Print keys at index 5
Node* temp = hashTable[5];
while(temp != NULL) {
  printf("%d -> ", temp->key);
  temp = temp->next;
}
printf("NULL\n");
medium
A. NULL
B. 5 -> 15 -> 25 -> NULL
C. 15 -> 25 -> 5 -> NULL
D. 25 -> 15 -> 5 -> NULL

Solution

  1. Step 1: Calculate hash index for each key

    All keys 5, 15, 25 % 10 = 5, so all go to index 5.
  2. Step 2: Trace insertion order in chaining

    Each new node is inserted at the head, so order is 25 (last inserted), then 15, then 5.
  3. Final Answer:

    25 -> 15 -> 5 -> NULL -> Option D
  4. Quick Check:

    New nodes added at head = 25 -> 15 -> 5 [OK]
Hint: New nodes go to head of list in chaining [OK]
Common Mistakes:
  • Assuming insertion at tail instead of head
  • Miscomputing hash index
  • Printing keys in wrong order
4. The following code for inserting into a chained hash table has a bug. What is the bug?
void insert(int key) {
  int index = key % 10;
  Node newNode;
  newNode.key = key;
  newNode.next = hashTable[index];
  hashTable[index] = &newNode;
}
medium
A. No bug, code is correct
B. newNode is a local variable and will be lost after function ends
C. next pointer is not assigned properly
D. Hash function is incorrect

Solution

  1. Step 1: Identify variable scope issue

    newNode is declared as a local variable on the stack inside insert function.
  2. Step 2: Explain why this causes a bug

    After function returns, newNode memory is invalid, so hashTable points to invalid memory.
  3. Final Answer:

    newNode is a local variable and will be lost after function ends -> Option B
  4. Quick Check:

    Local variable address stored = invalid pointer [OK]
Hint: Use malloc for nodes, not local variables [OK]
Common Mistakes:
  • Ignoring variable lifetime
  • Thinking hash function is wrong
  • Assuming next pointer assignment is incorrect
5. You have a hash table of size 7 using chaining. You insert keys: 10, 21, 17, 14, 28. The hash function is key % 7. After all insertions, which index will have the longest chain and what will be the order of keys in that chain if new nodes are inserted at the head?
hard
A. Index 0: 28 -> 14 -> 21 -> NULL
B. Index 3: 17 -> 14 -> 28 -> NULL
C. Index 3: 14 -> 17 -> 28 -> NULL
D. Index 4: 10 -> 21 -> NULL

Solution

  1. Step 1: Compute hash index for each key

    10 % 7 = 3, 21 % 7 = 0, 17 % 7 = 3, 14 % 7 = 0, 28 % 7 = 0.
  2. Step 2: Group keys by index and order insertion at head

    Index 0 keys inserted in order: 21, then 14, then 28 (last inserted at head). So chain at index 0 is 28 -> 14 -> 21 -> NULL.
    Index 3 keys are 10 and 17.
  3. Final Answer:

    Index 0: 28 -> 14 -> 21 -> NULL -> Option A
  4. Quick Check:

    Longest chain at index 0 with keys inserted at head [OK]
Hint: Calculate key % size, insert new nodes at head [OK]
Common Mistakes:
  • Miscomputing hash indexes
  • Mixing up insertion order in chains
  • Choosing wrong index for longest chain
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard