Collision handling is a method to resolve the problem when two keys hash to the same index in a hash table.
Click to reveal answer
beginner
Explain chaining as a collision handling technique.
Chaining stores all elements that hash to the same index in a linked list or chain at that index, allowing multiple elements to share one slot.
Click to reveal answer
beginner
What data structure is commonly used in chaining to handle collisions?
A linked list is commonly used to store multiple elements at the same hash index in chaining.
Click to reveal answer
intermediate
How does chaining affect the search operation in a hash table?
Search involves hashing to find the index, then traversing the linked list at that index to find the element, which may take longer if many collisions occur.
Click to reveal answer
intermediate
What is the average time complexity of search in a hash table using chaining?
The average time complexity is O(1 + α), where α is the load factor (number of elements divided by number of buckets).
Click to reveal answer
What happens when two keys hash to the same index in chaining?
ABoth keys are stored in a linked list at that index
BThe second key replaces the first key
CThe hash table resizes immediately
DThe keys are stored in separate arrays
✗ Incorrect
In chaining, all keys that hash to the same index are stored in a linked list at that index.
Which data structure is NOT typically used in chaining?
ALinked list
BArray
CBinary tree
DHash table
✗ Incorrect
Chaining uses linked lists or sometimes trees, but not another hash table inside each bucket.
What is the main advantage of chaining over open addressing?
AHandles collisions without probing
BUses less memory
CAlways faster search
DNo need for hash function
✗ Incorrect
Chaining handles collisions by storing multiple elements in a list, avoiding probing for empty slots.
If the load factor increases, what happens to the average search time in chaining?
AIt decreases
BIt stays the same
CIt increases
DIt becomes zero
✗ Incorrect
Higher load factor means longer chains, so average search time increases.
In chaining, what is stored at each index of the hash table?
AA single element
BA linked list of elements
CA sorted array
DA binary search tree
✗ Incorrect
Each index stores a linked list of elements that hash to that index.
Describe how chaining handles collisions in a hash table.
Think about storing multiple items in one bucket.
You got /4 concepts.
Explain the impact of load factor on the performance of chaining.
Consider how crowded the buckets get.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind collision handling using chaining in a hash table?
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: 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 A
Quick Check:
Chaining = linked list at index [OK]
Hint: Chaining means linked list at each hash index [OK]
Common Mistakes:
Thinking chaining replaces old elements
Confusing chaining with open addressing
Assuming chaining resizes table on collision
2. Which of the following TypeScript code snippets correctly initializes a hash table with chaining?
easy
A. const table: Array = new Array(10).fill([]);
B. const table: Array<number> = Array(10).map(() => []);
C. const table: Array<number> = new Array(10).fill(0);
Step 1: Understand how to initialize array of arrays in TypeScript
Using Array.from with a function creates separate arrays for each slot.
Step 2: Analyze each option
const table: Array = new Array(10).fill([]); uses fill([]) 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(() => []); uses map on uninitialized array, which won't work.
Hint: Avoid fill([]); use Array.from with function for unique arrays [OK]
Common Mistakes:
Thinking hash function is wrong
Trying to fix by changing push to concat
Ignoring array reference sharing issue
5. You have a hash table of size 4 using chaining. You insert keys 2, 6, 10, 14, and 18. Then you want to remove key 10. Which of the following shows the correct state of the hash table chains after removal?
Hash function: key % 4
hard
A. [[2, 6, 14, 18], [ ], [ ], [ ]] with 10 removed
B. [[2, 6, 14, 18], [ ], [ ], [ ]]
C. [[ ], [ ], [2, 6, 14, 18], [ ]] with 10 removed