Collision handling (open addressing) in Data Structures Theory - Time & Space Complexity
When storing data in a hash table, sometimes two items want the same spot. Open addressing is one way to handle this by finding another spot.
We want to know how the time to find or insert items grows as the table fills up.
Analyze the time complexity of inserting an item using linear probing in open addressing.
function insert(key, table) {
let index = hash(key) % table.size;
while (table[index] !== undefined) {
index = (index + 1) % table.size; // move to next slot
}
table[index] = key;
}
This code tries to place a key in the hash table. If the spot is taken, it checks the next spot until it finds an empty one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking table slots one by one (linear probing).
- How many times: Up to the number of slots checked until an empty one is found, which depends on how full the table is.
As the table fills, more slots may be checked before finding an empty one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (mostly empty) | About 1 to 2 checks |
| 100 (half full) | Several checks, maybe 5 to 10 |
| 1000 (almost full) | Many checks, possibly hundreds |
Pattern observation: The more full the table, the longer it takes to find an empty slot, so time grows as the load increases.
Time Complexity: O(n)
This means in the worst case, you might check many slots one by one, so time grows linearly with the number of items.
[X] Wrong: "Insertion always takes constant time because hashing is fast."
[OK] Correct: When many slots are filled, you may need to check many places before finding an empty one, making insertion slower.
Understanding how open addressing affects time helps you explain trade-offs in hash tables clearly and shows you grasp real-world data structure behavior.
What if we changed linear probing to quadratic probing? How would the time complexity change?
Practice
open addressing in collision handling?Solution
Step 1: Understand collision handling methods
Open addressing handles collisions by searching for another empty slot inside the hash table itself.Step 2: Compare with other methods
Unlike chaining (which uses linked lists), open addressing keeps all data inside the array by probing for free spots.Final Answer:
Find another empty spot in the hash table when a collision occurs -> Option AQuick Check:
Open addressing = find empty spot inside table [OK]
- Confusing open addressing with chaining
- Thinking collisions are ignored
- Assuming resizing happens immediately on collision
i in a hash table of size m?Solution
Step 1: Recall linear probing formula
Linear probing moves to the next slot by adding 1 to the current index and wrapping around using modulo.Step 2: Apply modulo with table size
To stay within the table size, use (i + 1) % m, where m is the table size.Final Answer:
next_index = (i + 1) % m -> Option CQuick Check:
Linear probing = increment index by 1 modulo table size [OK]
- Using multiplication instead of addition
- Incorrect modulo divisor
- Subtracting instead of adding
h(k) = k % 5, if keys 7, 12, and 17 are inserted using linear probing, what will be the final index of key 17?Solution
Step 1: Calculate initial hash indices
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 3 (free). Insert 12 at 3. For 17, indices 2 and 3 are taken, try 4 (free). Insert 17 at 4.Final Answer:
4 -> Option DQuick Check:
Linear probing moves to next free slot: 17 at index 4 [OK]
- Assuming all keys go to same index
- Not probing after collision
- Miscomputing modulo
table = [None]*5
key = 12
index = key % 5
while table[index] is not None:
index = (index + 1) % 5
# Missing insertion step hereSolution
Step 1: Analyze the loop behavior
The loop finds the next free slot by incrementing index until it finds None.Step 2: Check for insertion after loop
After the loop, the code does not insert the key into the found free slot, so the key is never stored.Final Answer:
It never inserts the key into the table -> Option AQuick Check:
Missing insertion step means key not stored [OK]
- Forgetting to insert after probing
- Ignoring full table infinite loop
- Wrong modulo usage
Solution
Step 1: Understand open addressing limits
Open addressing requires an empty slot to insert a new key by probing.Step 2: Consider full table scenario
If the table is full, no empty slot exists, so insertion cannot proceed and must fail or trigger resizing externally.Final Answer:
The insertion fails because no empty slot is available -> Option BQuick Check:
Full table means no free slot, insertion fails [OK]
- Assuming automatic resizing always happens
- Thinking keys get overwritten silently
- Ignoring infinite loop possibility
Ready to prove it?
Pass the test and this pattern moves unknown
