Collision Handling Using Open Addressing Linear Probing in DSA C# - Time & Space Complexity
We want to understand how long it takes to find or insert items when collisions happen in a hash table using linear probing.
How does the time grow as the table fills up?
Analyze the time complexity of the following code snippet.
public int LinearProbingSearch(int[] table, int key) {
int size = table.Length;
int index = key % size;
int startIndex = index;
while (table[index] != -1 && table[index] != key) {
index = (index + 1) % size;
if (index == startIndex) return -1; // full loop
}
return table[index] == key ? index : -1;
}
This code searches for a key in a hash table using linear probing to handle collisions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that checks each slot in the table one by one.
- How many times: Up to the size of the table in the worst case, because it may check every slot once.
As the table size grows, the number of checks can grow up to the table size if many slots are filled.
| 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 search or insert operation may check every slot once before finding the key or an empty slot.
[X] Wrong: "Linear probing always finds the key in constant time because it just moves one step at a time."
[OK] Correct: If the table is nearly full or many collisions happen, it may need to check many slots, making the time grow with the table size.
Understanding how collision handling affects search time helps you explain trade-offs in hash tables clearly and confidently.
"What if we used quadratic probing instead of linear 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 is a method where if a collision happens, the algorithm checks the next slot in the array sequentially.Step 2: Compare options with linear probing definition
If a collision occurs, check the next slot sequentially until an empty slot is found. correctly describes checking the next slot sequentially until an empty slot is found, which matches linear probing.Final Answer:
If a collision occurs, check the next slot sequentially until an empty slot is found. -> Option BQuick Check:
Linear probing = check next slot sequentially [OK]
- Confusing linear probing with chaining
- Thinking linear probing replaces existing elements
- Assuming table size doubles on collision
tableSize when a collision occurs at index?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 to wrap around.Step 2: Match formula with options
nextIndex = (index + 1) % tableSize; uses (index + 1) % tableSize, which is the correct formula for linear probing.Final Answer:
nextIndex = (index + 1) % tableSize; -> Option CQuick Check:
Linear probing next index = (index + 1) % size [OK]
- Multiplying index instead of adding
- Subtracting instead of adding
- Adding a number other than 1
h(key) = key % 5. Insert keys in this order: 7, 12, 17. What will be the final state of the hash table (index: value)?Solution
Step 1: Calculate initial indices for each key
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 index 3 (empty), insert 12. Insert 17: index 2 and 3 occupied, try index 4 (empty), insert 17.Final Answer:
{0: null, 1: null, 2: 7, 3: 12, 4: 17} -> Option AQuick Check:
Keys collide at 2, placed at 2,3,4 sequentially [OK]
- Placing all keys at index 2 ignoring collisions
- Not wrapping around the table
- Inserting keys in wrong order
int tableSize = 5;
int[] hashTable = new int[tableSize];
for (int i = 0; i < tableSize; i++) hashTable[i] = -1;
int Hash(int key) => key % tableSize;
void Insert(int key) {
int index = Hash(key);
while (hashTable[index] != -1) {
index = (index + 1);
}
hashTable[index] = key;
}
Insert(7);
Insert(12);
Insert(17);Solution
Step 1: Analyze the index update in the loop
The code increments index by 1 on collision but does not use modulo to wrap around the table size.Step 2: Identify the consequence of missing modulo
Without modulo, index can go beyond array bounds causing runtime error or incorrect behavior.Final Answer:
The code does not wrap index back to 0 when it reaches the end of the table. -> Option DQuick Check:
Linear probing requires modulo to wrap index [OK]
- Forgetting modulo causes index out of range
- Changing hash function unnecessarily
- Thinking initial values must be zero
h(key) = key % 7. You insert keys: 10, 24, 31, 17, 3. Then you delete key 24 by marking its slot as empty. Now you want to search for key 31. What problem might occur and how to fix it?Solution
Step 1: Understand deletion effect in linear probing
Deleting by marking slot empty breaks the probe chain, so search may stop early and miss keys inserted after the deleted slot.Step 2: Identify correct deletion method
Use a special 'deleted' marker to indicate slot is free for insertion but search should continue probing.Final Answer:
Search may stop early at the empty slot of deleted key 24, missing key 31; fix by using a special 'deleted' marker instead of empty. -> Option AQuick Check:
Deleted slots need special marker to continue search [OK]
- Marking deleted slots as empty breaks search
- Assuming search always finds keys after deletion
- Changing hash function unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
