Collision Handling Using Open Addressing Linear Probing in DSA Python - Time & Space Complexity
When we use open addressing with linear probing to handle collisions in a hash table, we want to know how long it takes to insert or find an item.
We ask: How does the time grow as the table fills up?
Analyze the time complexity of the following code snippet.
class HashTable:
def __init__(self, size):
self.size = size
self.table = [None] * size
def insert(self, key):
index = key % self.size
while self.table[index] is not None:
index = (index + 1) % self.size
self.table[index] = key
This code inserts keys into a hash table using linear probing to find the next empty slot when a collision happens.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that checks for the next empty slot when a collision occurs.
- How many times: In the worst case, it can check almost all slots if the table is nearly full.
When the table is mostly empty, the loop runs very few times. As the table fills, the loop runs more times to find an empty slot.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (10% full) | About 1 check per insert |
| 100 (50% full) | Several checks per insert |
| 1000 (90% full) | Many checks, close to 1000 in worst case |
Pattern observation: The more full the table, the longer it takes to find an empty slot, growing roughly linearly with the number of filled slots.
Time Complexity: O(n)
This means in the worst case, the time to insert or search grows linearly with the number of items in the table.
[X] Wrong: "Insertion always takes constant time because hashing is constant time."
[OK] Correct: When collisions happen and the table is full or nearly full, linear probing must check many slots, making insertion slower.
Understanding how collision handling affects time helps you explain trade-offs in hash tables clearly and confidently during interviews.
"What if we changed linear probing to quadratic probing? How would the time complexity change?"
Practice
linear probing in collision handling for hash tables?Solution
Step 1: Understand linear probing concept
Linear probing handles collisions by checking the next slot in order if the original slot is taken.Step 2: Compare with other methods
Other methods like chaining use linked lists, resizing, or rehashing, which are different from linear probing.Final Answer:
If a slot is occupied, check the next slot sequentially until an empty one is found. -> Option BQuick Check:
Linear probing = sequential slot check [OK]
- Confusing linear probing with chaining
- Thinking linear probing resizes the table
- Assuming rehashing is part of linear probing
m when a collision occurs at index i?Solution
Step 1: Recall linear probing formula
Linear probing moves to the next slot by adding 1 to the current index and taking modulo with table size.Step 2: Apply modulo for wrap-around
Using modulo ensures the index wraps around to the start if it reaches the end of the table.Final Answer:
(i + 1) % m -> Option CQuick Check:
Next index = (current + 1) mod size [OK]
- Adding 2 instead of 1 for next index
- Multiplying index instead of adding
- Not using modulo for wrap-around
h(k) = k % 5. Insert keys 7, 12, 17 in order. What is the final state of the table?Solution
Step 1: Calculate initial positions
h(7) = 7 % 5 = 2, h(12) = 12 % 5 = 2, h(17) = 17 % 5 = 2.Step 2: Insert keys using linear probing
Insert 7 at index 2. For 12, index 2 is taken, so try index 3. Insert 12 at index 3. For 17, indices 2 and 3 are taken, so insert at index 4.Final Answer:
[None, None, 7, 12, 17] -> Option AQuick Check:
Keys placed at 2, 3, 4 with linear probing [OK]
- Placing all keys at same index ignoring collisions
- Not probing to next slot after collision
- Miscomputing hash values
def insert(table, key):
m = len(table)
index = key % m
while table[index] is not None:
index = (index + 1) % m
table[index] = key
hash_table = [None]*5
insert(hash_table, 5)
insert(hash_table, 10)
insert(hash_table, 15)
insert(hash_table, 20)
insert(hash_table, 25)
insert(hash_table, 30)Solution
Step 1: Analyze insertion loop
The while loop continues probing until it finds an empty slot but has no check for full table.Step 2: Identify problem when table is full
If all slots are occupied, the loop cycles endlessly, causing infinite loop.Final Answer:
Infinite loop if table is full because no stop condition in while loop. -> Option AQuick Check:
Full table causes infinite loop without exit [OK]
- Assuming hash function is wrong
- Ignoring infinite loop possibility
- Thinking dynamic size fixes this bug
h(k) = k % 7. After all insertions, what is the index of key 42 in the table?Solution
Step 1: Calculate initial hash indices
h(14)=0, h(21)=0, h(28)=0, h(35)=0, h(42)=0 (all keys hash to index 0).Step 2: Insert keys using linear probing
Insert 14 at 0. For 21, index 0 taken, try 1. Insert 21 at 1. For 28, indices 0 and 1 taken, insert at 2. For 35, insert at 3. For 42, insert at 4.Step 3: Find index of key 42
Key 42 is at index 4.Final Answer:
4 -> Option DQuick Check:
42 placed at first free slot after collisions [OK]
- Assuming key stays at initial hash index
- Not probing sequentially after collisions
- Miscounting index after multiple collisions
Ready to prove it?
Pass the test and this pattern moves unknown
