Collision happens when two different keys get the same index in the hash table. This means they want to be stored in the same spot.
Click to reveal answer
beginner
Explain chaining as a collision handling method.
Chaining stores all elements that collide at the same index in a linked list. So, each slot in the hash table points to a list of items that share that index.
Click to reveal answer
intermediate
How does chaining affect search time in a hash table?
Search time depends on the length of the linked list at the index. If many items collide, the list grows and search takes longer, but on average it stays fast if the table is sized well.
Click to reveal answer
intermediate
What is the main advantage of using chaining over open addressing?
Chaining can handle any number of collisions without needing to resize the table immediately. It also makes deletion easier because items are stored in linked lists.
Click to reveal answer
beginner
Describe the structure used in chaining for collision handling.
Each slot in the hash table contains a pointer to the head of a linked list. Each node in the list holds a key-value pair and a pointer to the next node.
Click to reveal answer
What happens when two keys hash to the same index in chaining?
AOne key overwrites the other
BThe table resizes immediately
CThey are stored in a linked list at that index
DThe second key is discarded
✗ Incorrect
In chaining, all keys that collide are stored in a linked list at the same index.
Which data structure is commonly used in chaining to handle collisions?
AQueue
BArray
CStack
DLinked list
✗ Incorrect
Chaining uses linked lists to store multiple elements at the same hash index.
What is a disadvantage of chaining?
AIt wastes memory by storing empty slots
BSearch time can degrade if many collisions occur
CIt cannot handle collisions
DIt does not allow deletion
✗ Incorrect
If many keys collide, the linked list grows longer, making search slower.
In chaining, what does each node in the linked list store?
AKey, value, and pointer to next node
BOnly the value
COnly the key
DIndex of the hash table
✗ Incorrect
Each node stores the key, value, and a pointer to the next node in the list.
Why is chaining preferred when the load factor is high?
ABecause it avoids resizing the table immediately
BBecause it uses less memory
CBecause it stores keys in arrays
DBecause it prevents collisions
✗ Incorrect
Chaining allows multiple keys at the same index, so the table does not need to resize immediately when many keys are inserted.
Explain how collision handling using chaining works in a hash table.
Think about what happens when two keys want the same spot.
You got /4 concepts.
Describe the advantages and disadvantages of using chaining for collision handling.
Consider both performance and memory aspects.
You got /4 concepts.
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
Step 1: Understand collision in hash tables
When two keys hash to the same index, a collision occurs.
Step 2: Explain chaining method
Chaining stores all collided elements in a linked list at that index, allowing multiple elements per slot.
Final Answer:
Store all elements that hash to the same index in a linked list at that index -> Option A
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
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.
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.
Final Answer:
typedef struct Node { int key; struct Node* next; } Node; -> Option C
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?
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
Step 1: Identify variable scope issue
newNode is declared as a local variable on the stack inside insert function.
Step 2: Explain why this causes a bug
After function returns, newNode memory is invalid, so hashTable points to invalid memory.
Final Answer:
newNode is a local variable and will be lost after function ends -> Option B
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?
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.
Final Answer:
Index 0: 28 -> 14 -> 21 -> NULL -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.