Recall & Review
beginner
What is collision in a hash table?
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?
✗ 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?
✗ Incorrect
Chaining stores all collided keys in a list at the same index.
What is the main advantage of chaining over open addressing?
✗ Incorrect
Chaining stores collided elements in a list, avoiding probing other slots.
What data structure is commonly used to implement chaining?
✗ 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?
✗ 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.