Collision Handling Using Open Addressing Linear Probing in DSA C++ - 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 find or insert an item.
We ask: How does the time grow as the table fills up?
Analyze the time complexity of the following code snippet.
int linearProbingInsert(int hashTable[], int size, int key) {
int index = key % size;
int startIndex = index;
while (hashTable[index] != -1) {
index = (index + 1) % size;
if (index == startIndex) return -1; // Table full
}
hashTable[index] = key;
return index;
}
This code tries to insert a key into a hash table using linear probing to find an empty spot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that checks each slot in the table until it finds an empty one.
- How many times: In the worst case, it can check every slot once, up to the size of the table.
As the table gets fuller, the number of checks before finding an empty slot grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of operations can grow linearly with the table size in the worst case.
Time Complexity: O(n)
This means in the worst case, the time to insert or search grows directly with the number of slots in the table.
[X] Wrong: "Linear probing always takes constant time because it just checks one slot at a time."
[OK] Correct: When many slots are filled, it may need to check many slots in a row, making the time grow with the table size.
Understanding how linear probing affects time helps you explain hash table performance clearly and shows you know how collisions impact speed.
"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 occupied.Step 2: Compare with other methods
Other options describe chaining or resizing, not linear probing.Final Answer:
If a collision occurs, check the next slot sequentially until an empty slot is found. -> Option DQuick Check:
Linear probing = sequential slot check [OK]
- Confusing linear probing with chaining
- Thinking collision means resizing immediately
- Assuming random slot probing
m when current index is i?Solution
Step 1: Recall linear probing formula
Linear probing moves to the next slot by adding 1 to the current index, wrapping around using modulo.Step 2: Verify modulo usage
Modulo with table sizemensures index stays within bounds.Final Answer:
(i + 1) % m -> Option BQuick Check:
Next index = (current + 1) mod size [OK]
- Using multiplication instead of addition
- Not using modulo to wrap index
- Subtracting instead of adding
h(k) = k % 7. Insert keys 10, 20, 15 in order. What is the final state of the table (index: value)?Solution
Step 1: Compute hashes and insert 10
h(10) = 10 % 7 = 3, index 3 empty, insert 10 at 3.Step 2: Insert 20
h(20) = 20 % 7 = 6, index 6 empty, insert 20 at 6.Step 3: Insert 15
h(15) = 15 % 7 = 1, index 1 empty, insert 15 at 1.Final Answer:
0: null, 1: 15, 2: null, 3: 10, 4: null, 5: null, 6: 20 -> Option AQuick Check:
Positions 1:15, 3:10, 6:20 [OK]
- Miscomputing hash values
- Not applying modulo correctly
- Placing keys at wrong indices
int hash(int key, int size) { return key % size; }
void insert(int table[], int size, int key) {
int index = hash(key, size);
while (table[index] != -1) {
index = (index + 1) % size;
}
table[index] = key;
}
int main() {
int size = 5;
int table[5] = {-1, -1, -1, -1, -1};
insert(table, size, 5);
insert(table, size, 10);
insert(table, size, 15);
insert(table, size, 20);
insert(table, size, 25);
insert(table, size, 30);
return 0;
}Solution
Step 1: Analyze insertion loop
The while loop probes next index until an empty slot (-1) is found.Step 2: Check for full table scenario
If the table is full, the loop will cycle endlessly without a stop condition.Step 3: Confirm other options
Hash function is correct modulo. Array initialization with -1 is valid. So only infinite loop is the bug.Final Answer:
Infinite loop possible when table is full because no stop condition in while loop. -> Option AQuick Check:
Full table causes infinite loop in linear probing [OK]
- Ignoring full table infinite loop
- Thinking hash function is wrong
- Assuming array init with -1 is invalid
h(k) = k % 10. If index 2 is occupied, what sequence of indices will be checked before insertion? What index will key 12 be inserted at if indices 2 to 6 are occupied but index 7 is free?Solution
Step 1: Calculate initial index for key 12
h(12) = 12 % 10 = 2, so start checking at index 2.Step 2: Apply linear probing sequence
Since indices 2, 3, 4, 5, 6 are occupied, check next indices sequentially: 2, 3, 4, 5, 6, then 7.Step 3: Find first free slot
Index 7 is free, so insert key 12 at index 7.Final Answer:
Check indices 2, 3, 4, 5, 6, 7; insert at 7 -> Option CQuick Check:
Linear probe checks sequential slots until free [OK]
- Skipping indices in probing
- Inserting at first occupied slot
- Not wrapping index with modulo
Ready to prove it?
Pass the test and this pattern moves unknown
