Challenge - 5 Problems
Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inserting keys with chaining
What is the state of the hash table after inserting keys 10, 22, 31, 4, 15 using chaining with hash function h(key) = key % 10?
DSA Python
class HashTable: def __init__(self): self.size = 10 self.table = [[] for _ in range(self.size)] def hash_function(self, key): return key % self.size def insert(self, key): index = self.hash_function(key) self.table[index].append(key) ht = HashTable() keys = [10, 22, 31, 4, 15] for key in keys: ht.insert(key) print(ht.table)
Attempts:
2 left
💡 Hint
Remember the hash function is key % 10, and collisions are handled by adding keys to the list at that index.
✗ Incorrect
Hashes: 10%10=0, 22%10=2, 31%10=1, 4%10=4, 15%10=5. The table becomes [[10], [31], [22], [], [4], [15], [], [], [], []].
❓ Predict Output
intermediate2:00remaining
Output after deleting a key in chaining hash table
Given the hash table with chaining below, what is the state after deleting key 22?
Initial table:
[[10], [31], [22, 42], [], [4], [15], [], [], [], []]
Hash function: h(key) = key % 10
DSA Python
class HashTable: def __init__(self): self.size = 10 self.table = [[10], [31], [22, 42], [], [4], [15], [], [], [], []] def hash_function(self, key): return key % self.size def delete(self, key): index = self.hash_function(key) if key in self.table[index]: self.table[index].remove(key) ht = HashTable() ht.delete(22) print(ht.table)
Attempts:
2 left
💡 Hint
Deleting a key removes it from the list at the hashed index.
✗ Incorrect
Key 22 is in the list at index 2. Removing it leaves [42] at index 2.
🔧 Debug
advanced2:00remaining
Identify the error in chaining insertion
What error does the following code raise when inserting keys 5 and 15?
Code:
class HashTable:
def __init__(self):
self.size = 10
self.table = [None] * self.size
def hash_function(self, key):
return key % self.size
def insert(self, key):
index = self.hash_function(key)
self.table[index].append(key)
ht = HashTable()
ht.insert(5)
ht.insert(15)
DSA Python
class HashTable: def __init__(self): self.size = 10 self.table = [None] * self.size def hash_function(self, key): return key % self.size def insert(self, key): index = self.hash_function(key) self.table[index].append(key) ht = HashTable() ht.insert(5) ht.insert(15)
Attempts:
2 left
💡 Hint
Check how the table is initialized and what type each slot holds before insertion.
✗ Incorrect
The table slots are initialized to None, so calling append on None raises AttributeError.
🧠 Conceptual
advanced1:30remaining
Why use chaining for collision handling?
Which of the following is the main advantage of using chaining to handle collisions in a hash table?
Attempts:
2 left
💡 Hint
Think about what happens when two keys hash to the same index.
✗ Incorrect
Chaining stores collided keys in a list at the same index, preventing overwriting.
❓ Predict Output
expert2:30remaining
Final hash table state after mixed operations with chaining
Given the hash table with chaining and hash function h(key) = key % 7, what is the state of the table after these operations?
Operations:
Insert 10
Insert 21
Insert 14
Insert 7
Delete 21
Insert 28
Delete 14
Initial table is empty with size 7.
DSA Python
class HashTable: def __init__(self): self.size = 7 self.table = [[] for _ in range(self.size)] def hash_function(self, key): return key % self.size def insert(self, key): index = self.hash_function(key) self.table[index].append(key) def delete(self, key): index = self.hash_function(key) if key in self.table[index]: self.table[index].remove(key) ht = HashTable() operations = [ ('insert', 10), ('insert', 21), ('insert', 14), ('insert', 7), ('delete', 21), ('insert', 28), ('delete', 14) ] for op, key in operations: if op == 'insert': ht.insert(key) else: ht.delete(key) print(ht.table)
Attempts:
2 left
💡 Hint
Calculate the hash index for each key and update the table step-by-step.
✗ Incorrect
Step-by-step (all colliding keys hash to index 0 except 10→3):
Insert 10: [[], [], [], [10], [], [], []]
Insert 21: [[21], [], [], [10], [], [], []]
Insert 14: [[21, 14], [], [], [10], [], [], []]
Insert 7: [[21, 14, 7], [], [], [10], [], [], []]
Delete 21: [[14, 7], [], [], [10], [], [], []]
Insert 28: [[14, 7, 28], [], [], [10], [], [], []]
Delete 14: [[7, 28], [], [], [10], [], [], []]