0
0
DSA Pythonprogramming~5 mins

Collision Handling Using Chaining in DSA Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ALinked lists or lists at each index
BOpen addressing
CRehashing the entire table
DIgnoring collisions
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
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
What data structure is commonly used to implement chaining?
AArray
BStack
CLinked list or list
DQueue
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
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.