0
0
DSA Pythonprogramming~20 mins

Collision Handling Using Chaining in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[[], [10, 22, 31, 4, 15], [], [], [], [], [], [], [], []]
B[[10], [22], [31], [4], [15], [], [], [], [], []]
C[[10, 22, 31, 4, 15], [], [], [], [], [], [], [], [], []]
D[[10], [31], [22], [], [4], [15], [], [], [], []]
Attempts:
2 left
💡 Hint
Remember the hash function is key % 10, and collisions are handled by adding keys to the list at that index.
Predict Output
intermediate
2: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)
A[[10], [31], [], [], [4], [15], [], [], [], []]
B[[10], [31], [42], [], [4], [15], [], [], [], []]
C[[10], [31], [22, 42], [], [4], [15], [], [], [], []]
D[[10], [31], [42, 22], [], [4], [15], [], [], [], []]
Attempts:
2 left
💡 Hint
Deleting a key removes it from the list at the hashed index.
🔧 Debug
advanced
2: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)
AAttributeError: 'NoneType' object has no attribute 'append'
BIndexError: list index out of range
CTypeError: unsupported operand type(s) for %: 'str' and 'int'
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Check how the table is initialized and what type each slot holds before insertion.
🧠 Conceptual
advanced
1: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?
AIt uses less memory than open addressing methods.
BIt guarantees constant time lookup for all keys regardless of collisions.
CIt allows multiple keys to be stored at the same index without overwriting existing keys.
DIt avoids the need for a hash function.
Attempts:
2 left
💡 Hint
Think about what happens when two keys hash to the same index.
Predict Output
expert
2: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)
A[[7, 28], [], [], [10], [], [], []]
B[[], [7], [], [10, 28], [], [], []]
C[[28], [7], [], [10], [], [], []]
D[[], [7], [], [10], [], [], [28]]
Attempts:
2 left
💡 Hint
Calculate the hash index for each key and update the table step-by-step.