Collision happens when two different keys get mapped to the same index in the hash table.
Click to reveal answer
beginner
What is chaining in collision handling?
Chaining is a method where each index of the hash table holds a list (or chain) of all elements that hash to the same index.
Click to reveal answer
intermediate
How does chaining help in handling collisions?
Chaining stores multiple elements at the same index using a linked list or list, so collisions do not overwrite data but are stored together.
Click to reveal answer
intermediate
What is the time complexity of search in a hash table using chaining in the average case?
Average case search time is O(1 + α), where α is the load factor (number of elements / number of buckets). Usually close to O(1) if load factor is low.
Click to reveal answer
beginner
Show a simple Python code snippet to insert a key-value pair using chaining in a hash table.
class HashTable:
def __init__(self, size):
self.size = size
self.table = [[] for _ in range(size)]
def hash_function(self, key):
return hash(key) % self.size
def insert(self, key, value):
index = self.hash_function(key)
# Check if key exists and update
for i, (k, v) in enumerate(self.table[index]):
if k == key:
self.table[index][i] = (key, value)
return
# If key not found, append
self.table[index].append((key, value))
Click to reveal answer
What does chaining use to handle collisions in a hash table?
ALinked lists or lists at each index
BOpen addressing
CRehashing the entire table
DIgnoring collisions
✗ Incorrect
Chaining stores collided elements in a list or linked list at the same index.
If two keys hash to the same index, what happens in chaining?
AOne key overwrites the other
BThe second key is discarded
CThe table resizes immediately
DBoth keys are stored in a chain (list) at that index
✗ Incorrect
Chaining stores all collided keys in a list at the same index.
What is the main advantage of chaining over open addressing?
ANo need to resize the table
BHandles collisions without probing other slots
CUses less memory
DFaster hash function
✗ Incorrect
Chaining stores collided elements in a list, avoiding probing other slots.
What data structure is commonly used to implement chaining?
AArray
BStack
CLinked list or list
DQueue
✗ Incorrect
Chaining uses linked lists or lists to store multiple elements at the same index.
What happens if the load factor increases in a hash table using chaining?
ASearch time increases because chains get longer
BSearch time decreases
CTable automatically shrinks
DCollisions stop occurring
✗ Incorrect
Higher load factor means longer chains, which increases search time.
Explain how collision handling using chaining works in a hash table.
Think about what happens when two keys map to the same place.
You got /4 concepts.
Describe the advantages and disadvantages of using chaining for collision handling.
Consider both performance and memory.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Store collided elements in a linked list at the same index
B. Replace the existing element with the new one
C. Resize the hash table immediately
D. Ignore the new element causing collision
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 to share the same slot.
Final Answer:
Store collided elements in a linked list at the same index -> Option A
Quick Check:
Chaining = linked list at index [OK]
Hint: Remember: chaining uses linked lists to store collisions [OK]
Common Mistakes:
Thinking collisions replace existing elements
Assuming collisions are ignored
Believing resizing happens immediately on collision
2. Which of the following is the correct way to initialize a hash table with chaining in Python?
easy
A. hash_table = dict()
B. hash_table = [None] * size
C. hash_table = [0] * size
D. hash_table = [[] for _ in range(size)]
Solution
Step 1: Understand chaining initialization
Chaining requires each slot to hold a list to store multiple elements.
Step 2: Choose correct initialization
Using list comprehension to create a list of empty lists ensures each slot can hold multiple elements.
Final Answer:
hash_table = [[] for _ in range(size)] -> Option D
Quick Check:
Empty lists per slot = hash_table = [[] for _ in range(size)] [OK]
Hint: Use list of empty lists for chaining slots [OK]
Common Mistakes:
Using [None] * size creates references to same list
Using zeros instead of lists for chaining
Using dict() which is not a list-based hash table
3. Consider the following code using chaining for collision handling:
hash_table = [[] for _ in range(3)]
keys = [5, 8, 11]
for key in keys:
index = key % 3
hash_table[index].append(key)
print(hash_table)
What is the output?
medium
A. [[5, 8, 11], [], []]
B. [[], [], [5, 8, 11]]
C. [[], [5, 8, 11], []]
D. [[], [5], [8, 11]]
Solution
Step 1: Calculate indices for each key
5 % 3 = 2, 8 % 3 = 2, 11 % 3 = 2. All keys map to index 2.
Step 2: Append keys to corresponding lists
All keys 5, 8, 11 are appended to hash_table[2], so hash_table = [[], [], [5, 8, 11]].
Final Answer:
[[], [], [5, 8, 11]] -> Option B
Quick Check:
Keys mod 3 = 2, all in last list [OK]
Hint: Calculate key % size to find index, then append [OK]
Common Mistakes:
Miscomputing modulo operation
Appending keys to wrong index
Confusing list positions in output
4. Find the error in this chaining insertion code: