Collision Handling Using Open Addressing Linear Probing in DSA Javascript - 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 items as the table fills up.
We ask: How does the time to find a spot or search grow as more items are added?
Analyze the time complexity of the following code snippet.
function linearProbingInsert(hashTable, key) {
let index = key % hashTable.length;
while (hashTable[index] !== null) {
index = (index + 1) % hashTable.length;
}
hashTable[index] = key;
}
This code tries to insert a key into a hash table using linear probing to find the next free spot if a collision happens.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that checks each slot in the hash table until it finds an empty one.
- How many times: In the worst case, it may check every slot once if the table is almost full.
As the hash table fills, the number of checks to find an empty slot grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (mostly empty) | 1 to 2 checks |
| 100 (half full) | several checks, maybe 5-10 |
| 1000 (almost full) | up to 1000 checks in worst case |
Pattern observation: When the table is mostly empty, insertion is fast. As it fills, the time to find a free spot grows roughly linearly with the number of items.
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 fast."
[OK] Correct: When collisions happen and the table is full or nearly full, linear probing may check many slots, making insertion slower.
Understanding how collision handling affects performance shows you grasp real-world data structure behavior, a key skill in coding interviews and software design.
"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 methods
Linear probing handles collisions by searching the next available slot in a sequence.Step 2: Identify linear probing behavior
When a collision happens, linear probing checks the next slot one by one until it finds an empty slot.Final Answer:
If a collision occurs, check the next slot sequentially until an empty slot is found. -> Option AQuick Check:
Linear probing = sequential slot check [OK]
- Confusing linear probing with chaining
- Thinking collision means resizing immediately
- Replacing existing values instead of probing
m when current index is i?Solution
Step 1: Recall linear probing index update
Linear probing moves to the next slot by adding 1 to the current index.Step 2: Apply modulo to wrap around
To stay within table size, use modulo operation with table sizem.Final Answer:
(i + 1) % m -> Option DQuick 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 % 5. Insert keys 7, 12, 17 in order. What is the final state of the table (index: value)?Solution
Step 1: Calculate initial positions using hash function
7 % 5 = 2, 12 % 5 = 2, 17 % 5 = 2. All keys initially map to index 2.Step 2: Insert keys using linear probing
Insert 7 at index 2 (empty). Insert 12: index 2 occupied, try 3 (empty), insert at 3. Insert 17: index 2 occupied, 3 occupied, try 4 (empty), insert at 4.Final Answer:
0: null, 1: null, 2: 7, 3: 12, 4: 17 -> Option AQuick Check:
Keys placed at 2, 3, 4 with probing [OK]
- Placing all keys at index 2 ignoring collisions
- Skipping slots incorrectly during probing
- Miscomputing hash values
const size = 7;
const table = new Array(size).fill(null);
function hash(key) {
return key % size;
}
function insert(key) {
let index = hash(key);
while (table[index] !== null) {
index = (index + 1) % size;
}
table[index] = key;
}
insert(10);
insert(17);
insert(24);
console.log(table);Solution
Step 1: Analyze insert function loop
The while loop probes next slots until it finds an empty slot (null).Step 2: Identify missing full table check
If the table is full, the loop cycles endlessly without exit condition, causing infinite loop.Final Answer:
The insert function does not handle the case when the table is full, causing an infinite loop. -> Option CQuick Check:
Missing full table check causes infinite loop [OK]
- Thinking modulo should be size-1
- Ignoring infinite loop possibility
- Confusing null with zero initialization
h(k) = k % 10. Which index will key 22 be inserted into?Solution
Step 1: Calculate initial index for key 22
22 % 10 = 2, so initial index is 2.Step 2: Apply linear probing to find empty slot
Indices 2, 3, 4, 5 are occupied. Next index is 6, which is free.Final Answer:
6 -> Option BQuick Check:
Start at 2, probe until free slot at 6 [OK]
- Inserting at first hashed index ignoring occupancy
- Stopping probing too early
- Choosing wrong next index
Ready to prove it?
Pass the test and this pattern moves unknown
