Collision Handling Using Chaining in DSA C++ - Time & Space Complexity
When we use chaining to handle collisions in a hash table, we want to know how fast operations like search, insert, and delete run.
We ask: How does the time to do these operations grow as the number of items increases?
Analyze the time complexity of searching for a key in a hash table using chaining.
// Search key in hash table with chaining
bool search(int key) {
int index = hashFunction(key);
Node* current = table[index];
while (current != nullptr) {
if (current->key == key) return true;
current = current->next;
}
return false;
}
This code looks in the linked list at the hashed index to find the key.
Look for loops or repeated steps that take time.
- Primary operation: Traversing the linked list at the hash index.
- How many times: Up to the length of the list at that index, which depends on collisions.
As more items are added, lists at each index can get longer if the table size stays the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 to 2 steps (short lists) |
| 100 | About 10 steps (longer lists) |
| 1000 | About 100 steps (even longer lists) |
Pattern observation: The time grows roughly in direct proportion to the number of items per list.
Time Complexity: O(n/k) where k is the number of buckets.
This means the time depends on how many items share the same bucket; more buckets mean faster operations.
[X] Wrong: "Searching in a chained hash table is always O(1)."
[OK] Correct: If many keys collide into the same bucket, the linked list grows, making search slower than constant time.
Understanding how chaining affects time helps you explain hash table performance clearly and shows you know how data structures behave in real use.
"What if we resized the hash table dynamically to keep the lists short? How would that affect the time complexity?"
Practice
collision handling using chaining in hash tables?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 to keep them separate.Final Answer:
Store all elements that hash to the same index in a linked list at that index -> Option AQuick Check:
Chaining = linked list at index [OK]
- Thinking chaining resizes the table
- Confusing chaining with open addressing
- Ignoring collisions instead of handling them
Solution
Step 1: Understand chaining data structure
Chaining uses a list at each index, so a vector of lists is needed.Step 2: Check syntax correctness
std::vector creates a vector where each element is a list, perfect for chaining.Final Answer:
std::vector hashTable(size); -> Option CQuick Check:
Vector of lists = chaining hash table [OK]
- Using list of vectors instead of vector of lists
- Using vector of ints only (no chaining)
- Incorrect container nesting order
std::vector hashTable(5);
int hashFunction(int key) { return key % 5; }
// Insert keys
hashTable[hashFunction(7)].push_back(7);
hashTable[hashFunction(12)].push_back(12);
hashTable[hashFunction(17)].push_back(17);
// Print contents at index 2
for (int val : hashTable[2]) {
std::cout << val << " ";
}What will be the output?
Solution
Step 1: Calculate hash indices for keys
7 % 5 = 2, 12 % 5 = 2, 17 % 5 = 2. All keys go to index 2.Step 2: Check what is printed at index 2
All three keys (7, 12, 17) are stored in the list at index 2 and printed in insertion order.Final Answer:
7 12 17 -> Option AQuick Check:
Keys mod 5 = 2, print all at index 2 [OK]
- Forgetting all keys collide at index 2
- Printing keys at wrong index
- Assuming keys overwrite each other
std::vector hashTable;
int hashFunction(int key) { return key % 10; }
void insertKey(int key) {
int index = hashFunction(key);
hashTable[index].push_back(key);
}
int main() {
insertKey(15);
insertKey(25);
return 0;
}What is the error and how to fix it?
Solution
Step 1: Identify cause of runtime error
hashTable is declared but not sized, so accessing hashTable[index] causes out-of-range error.Step 2: Fix by initializing hashTable size
Initialize hashTable with size 10 (e.g., std::vector hashTable(10);) before insertions.Final Answer:
hashTable is empty; initialize with size before insertions -> Option BQuick Check:
Vector size must match hash range [OK]
- Ignoring vector size causes crash
- Changing hash function unnecessarily
- Using wrong list methods
After all insertions, which index will have the longest chain and what will be its contents?
Use hash function:
index = key % 7.Solution
Step 1: Calculate hash indices for each key
10 % 7 = 3, 21 % 7 = 0, 17 % 7 = 3, 14 % 7 = 0, 28 % 7 = 0, 35 % 7 = 0.Step 2: Group keys by index
Index 0: 21, 14, 28, 35 (4 keys); Index 3: 10, 17 (2 keys).Step 3: Identify longest chain and contents
Index 0 has the longest chain with keys 21, 14, 28, 35 inserted in order.Final Answer:
Index 0: 21 -> 14 -> 28 -> 35 -> Option DQuick Check:
Keys mod 7 grouped; longest chain at index 0 [OK]
- Miscomputing modulo results
- Mixing up indices and keys
- Ignoring insertion order in chain
Ready to prove it?
Pass the test and this pattern moves unknown
