What is the main idea behind open addressing with linear probing in hash tables?
When a collision happens (two keys map to the same slot), linear probing checks the next slot in the table, moving one by one until it finds an empty slot to store the new key.
Click to reveal answer
beginner
Why do we use a hash function in open addressing?
A hash function converts a key into an index in the hash table. It helps quickly find where to store or look for data. In open addressing, it also helps find the starting point for probing.
Click to reveal answer
intermediate
What happens if the hash table is full when using linear probing?
If the table is full, linear probing cannot find an empty slot, so insertion fails. This is why keeping the table less than full (load factor < 1) is important.
Click to reveal answer
beginner
How does linear probing affect the search operation in a hash table?
Search starts at the hashed index and moves forward one slot at a time until it finds the key or an empty slot, which means the key is not present.
Click to reveal answer
intermediate
What is primary clustering in linear probing?
Primary clustering happens when many keys cluster together in consecutive slots, causing longer search times because linear probing checks these clusters sequentially.
Click to reveal answer
In linear probing, if a collision occurs at index 5, where will the algorithm check next?
AIndex 6
BIndex 4
CIndex 0
DIndex 5 again
✗ Incorrect
Linear probing checks the next slot in order, so after index 5, it checks index 6.
What is the main disadvantage of linear probing?
AIt causes primary clustering
BIt requires extra memory for linked lists
CIt cannot handle collisions
DIt uses double hashing
✗ Incorrect
Linear probing can cause primary clustering, where many keys group together, slowing down operations.
Which of these is NOT true about open addressing with linear probing?
AIt searches sequentially after a collision
BIt stores all keys inside the hash table array
CIt uses linked lists to handle collisions
DIt can fail if the table is full
✗ Incorrect
Linear probing does not use linked lists; it stores all keys in the array and searches sequentially.
When searching for a key using linear probing, what indicates the key is not in the table?
AReaching the end of the array without finding the key
BFinding the key at the hashed index
CFinding a slot with a different key
DFinding an empty slot during probing
✗ Incorrect
If an empty slot is found during probing, it means the key was never inserted.
What is the load factor in a hash table?
ANumber of keys stored
BRatio of filled slots to total slots
CSize of the hash table
DNumber of collisions occurred
✗ Incorrect
Load factor is the ratio of how many slots are filled compared to the total slots in the table.
Explain how linear probing handles collisions in a hash table.
Think about moving step-by-step to find an empty place.
You got /4 concepts.
Describe the problem of primary clustering and its effect on hash table performance.
Imagine a traffic jam of keys in the table.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind linear probing in collision handling for hash tables?
easy
A. Rehash all elements to a new table when a collision occurs.
B. Use a linked list at each slot to store multiple elements.
C. Double the size of the hash table when a collision happens.
D. If a collision occurs, check the next slot sequentially until an empty slot is found.
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 D
Quick Check:
Linear probing = sequential slot check [OK]
Hint: Linear probing means try next slot one by one [OK]
Common Mistakes:
Confusing linear probing with chaining
Thinking collision means resizing immediately
Assuming random slot probing
2. Which of the following is the correct way to calculate the next index in linear probing for a hash table of size m when current index is i?
easy
A. (i * 2) % m
B. (i + 1) % m
C. (i - 1) % m
D. (i + 3) % m
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 size m ensures index stays within bounds.
Final Answer:
(i + 1) % m -> Option B
Quick Check:
Next index = (current + 1) mod size [OK]
Hint: Next slot in linear probing is (i+1) mod table size [OK]
Common Mistakes:
Using multiplication instead of addition
Not using modulo to wrap index
Subtracting instead of adding
3. Consider a hash table of size 7 using linear probing. The hash function is h(k) = k % 7. Insert keys 10, 20, 15 in order. What is the final state of the table (index: value)?
Hint: Calculate hash, insert keys, probe next if occupied [OK]
Common Mistakes:
Miscomputing hash values
Not applying modulo correctly
Placing keys at wrong indices
4. The following code snippet attempts to insert keys into a hash table using linear probing. Identify the bug:
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;
}
medium
A. Infinite loop possible when table is full because no stop condition in while loop.
B. Hash function is incorrect; should use (key + 1) % size.
C. No bug, code works correctly for all insertions.
D. Array initialization with -1 is invalid in C++.
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 A
Quick Check:
Full table causes infinite loop in linear probing [OK]
Hint: Check if loop stops when table is full [OK]
Common Mistakes:
Ignoring full table infinite loop
Thinking hash function is wrong
Assuming array init with -1 is invalid
5. You have a hash table of size 10 using linear probing. The table currently stores keys at indices: 2, 3, 4, 5, 6. You want to insert key 12 where 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?
hard
A. Check indices 2, 4, 6, 8; insert at 8
B. Check indices 2, 3, 4; insert at 4
C. Check indices 2, 3, 4, 5, 6, 7; insert at 7
D. Check indices 2, 3, 4, 5, 6; insert at 2
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 C
Quick Check:
Linear probe checks sequential slots until free [OK]
Hint: Probe sequentially until free slot found [OK]
Common Mistakes:
Skipping indices in probing
Inserting at first occupied slot
Not wrapping index with modulo
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.