What if your storage box could magically handle many items wanting the same spot without any mess?
Why Collision Handling Using Chaining in DSA Typescript?
Imagine you have a big box where you want to store your toys by their colors. But sometimes, two toys have the same color, so they both want to go to the same spot in the box.
If you try to put both toys in the same spot without a plan, they get mixed up and it becomes hard to find them later.
Trying to put multiple toys in the same spot without organizing them makes it slow and confusing to find a specific toy.
You might have to search through everything in that spot, which wastes time and can cause mistakes.
Collision Handling Using Chaining solves this by creating a small list at each spot in the box.
When two toys want the same spot, they just line up in that spot's list, so you can easily find each toy without mixing them up.
class HashTable { table: (string | null)[] = new Array(10).fill(null); insert(key: string) { const index = this.hash(key); if (this.table[index] !== null) { // No plan for collision console.log('Collision!'); } else { this.table[index] = key; } } hash(key: string): number { return key.length % 10; } }
class HashTable { table: Array<string[]> = new Array(10).fill(null).map(() => []); insert(key: string) { const index = this.hash(key); this.table[index].push(key); } hash(key: string): number { return key.length % 10; } }
This method lets you store many items in the same spot safely and find them quickly without confusion.
Think of a school locker where many students have the same locker number but different compartments inside to keep their things organized.
Manual storage fails when multiple items want the same spot.
Chaining creates a list at each spot to handle multiple items.
This keeps data organized and easy to find even with collisions.
Practice
collision handling using chaining in a hash table?Solution
Step 1: Understand collision in hash tables
When two keys hash to the same index, a collision occurs.Step 2: How chaining handles collisions
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 AQuick Check:
Chaining = linked list at index [OK]
- Thinking chaining replaces old elements
- Confusing chaining with open addressing
- Assuming chaining resizes table on collision
Solution
Step 1: Understand how to initialize array of arrays in TypeScript
UsingArray.fromwith a function creates separate arrays for each slot.Step 2: Analyze each option
const table: Array = new Array(10).fill([]); usesfill([])which fills all slots with the same array reference (wrong). const table: Array = Array.from({ length: 10 }, () => []); correctly creates separate empty arrays. const table: Array<number> = new Array(10).fill(0); is a number array, not array of arrays. const table: Array<number> = Array(10).map(() => []); usesmapon uninitialized array, which won't work.Final Answer:
const table: Array = Array.from({ length: 10 }, () => []); -> Option DQuick Check:
Use Array.from with function for separate arrays [OK]
- Using fill([]) causes all slots to share one array
- Using map on uninitialized array returns empty
- Initializing as number array instead of array of arrays
class HashTable {
table: Array;
size: number;
constructor(size: number) {
this.size = size;
this.table = Array.from({ length: size }, () => []);
}
hash(key: number): number {
return key % this.size;
}
insert(key: number) {
const index = this.hash(key);
this.table[index].push(key);
}
}
const ht = new HashTable(5);
ht.insert(1);
ht.insert(6);
ht.insert(11);
ht.insert(3);
console.log(ht.table);What will be printed to the console?
Solution
Step 1: Calculate hash index for each inserted key
Using hash = key % 5:
1 % 5 = 1
6 % 5 = 1
11 % 5 = 1
3 % 5 = 3Step 2: Insert keys into chains at calculated indices
Index 1 chain: [1, 6, 11]
Index 3 chain: [3]
Other indices remain empty arrays.Final Answer:
[[], [1, 6, 11], [], [3], []] -> Option BQuick Check:
Keys grouped by modulo 5 index [OK]
- Miscomputing modulo results
- Inserting keys into wrong index
- Confusing array positions in output
class HashTable {
table: Array;
size: number;
constructor(size: number) {
this.size = size;
this.table = new Array(size).fill([]);
}
hash(key: number): number {
return key % this.size;
}
insert(key: number) {
const index = this.hash(key);
this.table[index].push(key);
}
}
const ht = new HashTable(3);
ht.insert(1);
ht.insert(4);
console.log(ht.table);What is the bug and how to fix it?
Solution
Step 1: Identify problem with fill([])
Usingfill([])assigns the same array reference to all slots, so pushing to one affects all.Step 2: Correct initialization method
UseArray.from({ length: size }, () => [])to create separate arrays for each slot.Final Answer:
Bug: All slots share the same array due to fill([]). Fix: Use Array.from with () => [] -> Option AQuick Check:
fill([]) shares references; Array.from creates unique arrays [OK]
- Thinking hash function is wrong
- Trying to fix by changing push to concat
- Ignoring array reference sharing issue
Hash function: key % 4
Solution
Step 1: Calculate hash indices for all keys
2 % 4 = 2
6 % 4 = 2
10 % 4 = 2
14 % 4 = 2
18 % 4 = 2
All keys hash to index 2.Step 2: Insert keys into chain at index 2 and remove 10
Initial chain at index 2: [2, 6, 10, 14, 18]
After removing 10: [2, 6, 14, 18]Step 3: Represent hash table chains
Index 2 chain: [2, 6, 14, 18]
Other indices: empty arraysStep 4: Correct representation
[[ ], [ ], [2, 6, 14, 18], [ ]] with 10 removed from index 2 chain.Final Answer:
[[ ], [ ], [2, 6, 14, 18], [ ]] with 10 removed -> Option CQuick Check:
All keys at index 2; 10 removed from chain [OK]
- Placing keys in wrong indices
- Forgetting to remove key 10
- Misreading hash function modulo
Ready to prove it?
Pass the test and this pattern moves unknown
