Collision Handling Using Open Addressing Linear Probing in DSA Typescript - Time & Space Complexity
When we use open addressing with linear probing to handle collisions in a hash table, we want to know how the time to insert or find an item changes as the table fills up.
We ask: How does the number of steps grow when the table gets more crowded?
Analyze the time complexity of the following code snippet.
function linearProbingInsert(table: (number | null)[], key: number): void {
const size = table.length;
let index = key % size;
while (table[index] !== null) {
index = (index + 1) % size;
}
table[index] = key;
}
This code inserts a key into a hash table using linear probing to find the next empty slot if a collision occurs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that checks each slot until an empty one is found.
- How many times: In the worst case, it can check every slot once if the table is almost full.
As the table fills, the number of checks grows because more slots are occupied, so the loop runs longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (mostly empty) | 1-2 checks |
| 100 (half full) | Up to 50 checks |
| 1000 (almost full) | Up to 1000 checks |
Pattern observation: The number of checks grows roughly proportional to how full the table is, so more items mean more steps.
Time Complexity: O(n)
This means in the worst case, the time to insert or find an item grows linearly with the number of items in the table.
[X] Wrong: "Insertion always takes constant time because hashing is fast."
[OK] Correct: When many collisions happen, linear probing may check many slots, making insertion slower as the table fills.
Understanding how collision handling affects time helps you explain trade-offs in hash tables clearly, a skill valued in real-world coding and 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 collision handling
When two keys hash to the same index, a collision occurs.Step 2: Explain linear probing method
Linear probing resolves collisions by checking the next slot in order until an empty slot is found.Final Answer:
If a collision occurs, check the next slot sequentially until an empty slot is found. -> Option CQuick Check:
Linear probing = sequential slot check [OK]
- Confusing linear probing with chaining
- Thinking collision means resizing immediately
- Replacing existing values on collision
size?Solution
Step 1: Understand linear probing index calculation
Linear probing moves to the next slot by adding 1 to the current index and wrapping around using modulo.Step 2: Check code correctness
let nextIndex = (currentIndex + 1) % size; correctly calculates nextIndex as (currentIndex + 1) % size, ensuring wrap-around.Final Answer:
let nextIndex = (currentIndex + 1) % size; -> Option DQuick Check:
Next index = (current + 1) mod size [OK]
- Multiplying index instead of adding
- Subtracting index causing negative values
- Not using modulo for wrap-around
h(key) = key % 5. Insert keys in this order: 7, 12, 17. What is the final state of the hash table (index: value)?Solution
Step 1: Calculate initial hash indices
7 % 5 = 2, 12 % 5 = 2, 17 % 5 = 2. All keys hash to index 2.Step 2: Insert keys using linear probing
Insert 7 at index 2. For 12, index 2 is occupied, try 3 (empty), insert 12. For 17, indices 2 and 3 occupied, try 4 (empty), insert 17.Final Answer:
{0: null, 1: null, 2: 7, 3: 12, 4: 17} -> Option AQuick Check:
All keys start at 2, linear probe fills next slots [OK]
- Placing all keys at index 2 ignoring collisions
- Not wrapping around the table
- Misplacing keys at wrong indices
const size = 5;
const table = new Array(size).fill(null);
function hash(key: number) { return key % size; }
function insert(key: number) {
let index = hash(key);
while (table[index] !== null) {
index = (index + 1) % size;
}
table[index] = key;
}
insert(5);
insert(10);
insert(15);
insert(20);
insert(25);
insert(30);Solution
Step 1: Analyze insert loop behavior
The while loop keeps moving index until an empty slot is found.Step 2: Identify missing full table check
If the table is full, the loop cycles endlessly without exit condition.Final Answer:
No check for full table causing infinite loop when table is full. -> Option AQuick Check:
Full table without exit causes infinite loop [OK]
- Assuming hash function returns negative
- Thinking null vs undefined causes loop
- Forgetting to update index inside loop
h(key) = key % 7. You insert keys: 14, 21, 28, 35, 42. Then you delete key 21. What is the correct way to handle deletion to avoid search errors in linear probing?Solution
Step 1: Understand deletion problem in linear probing
Setting slot to null breaks the probe chain, causing search failures for keys after deleted slot.Step 2: Use special 'deleted' marker
Marking slot as 'deleted' allows probing to continue while allowing new insertions.Final Answer:
Mark the slot as a special 'deleted' marker instead of null to continue probing. -> Option BQuick Check:
Deleted marker keeps probe chain intact [OK]
- Setting slot to null breaks search chain
- Rehashing entire table unnecessarily
- Ignoring deletion impact on probing
Ready to prove it?
Pass the test and this pattern moves unknown
