What is the main idea behind linear probing in collision handling?
Linear probing handles collisions by checking the next available slot in the array sequentially until an empty slot is found.
Click to reveal answer
beginner
In linear probing, what happens if the hash index is already occupied?
If the hash index is occupied, linear probing moves to the next index (current index + 1) and checks if it is free, repeating this until an empty slot is found.
Click to reveal answer
intermediate
What is a potential problem with linear probing and how can it affect performance?
Linear probing can cause clustering, where consecutive slots fill up, leading to longer search times and slower insertions.
Click to reveal answer
intermediate
How does linear probing differ from chaining in collision handling?
Linear probing stores all elements in the same array by finding the next free slot, while chaining uses linked lists at each index to store multiple elements.
Click to reveal answer
beginner
What is the stopping condition when searching for an empty slot using linear probing?
The search stops when an empty slot is found or when the entire array has been checked, indicating the table is full.
Click to reveal answer
What does linear probing do when a collision occurs at a hash index?
ADeletes the existing element
BCreates a linked list at the index
CRehashes the key with a different function
DChecks the next slot sequentially until an empty slot is found
✗ Incorrect
Linear probing searches the next slots one by one until it finds an empty slot.
Which problem is commonly associated with linear probing?
AClustering of elements causing longer search times
BMemory overflow due to linked lists
CInfinite loops during insertion
DKeys getting lost during insertion
✗ Incorrect
Linear probing can cause clustering, where many elements group together, slowing down operations.
If the hash table is full, what happens when using linear probing to insert a new element?
AInsertion fails after checking all slots
BThe element replaces the first slot
CThe element is stored in a linked list
DThe table automatically resizes
✗ Incorrect
If all slots are checked and none are free, insertion fails because the table is full.
How does linear probing find the correct slot for a key after a collision?
ABy using a secondary hash function
BBy jumping to a random index
CBy moving sequentially to the next index until an empty slot is found
DBy storing the key in a separate array
✗ Incorrect
Linear probing checks the next slots one by one in order.
Which of the following is NOT true about linear probing?
AIt uses open addressing to resolve collisions
BIt uses linked lists to store collided elements
CIt can cause clustering of elements
DIt searches sequentially for an empty slot
✗ Incorrect
Linear probing does not use linked lists; chaining uses linked lists.
Explain how linear probing handles collisions in a hash table.
Think about moving step-by-step to find a free spot.
You got /4 concepts.
Describe the main disadvantage of linear probing and its impact on hash table performance.
Consider what happens when many elements group together.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind Linear Probing in collision handling for hash tables?
easy
A. Use a linked list at each slot to store multiple elements.
B. If a collision occurs, check the next slot sequentially until an empty slot is found.
C. Double the size of the hash table when a collision happens.
D. Replace the existing element in the slot with the new one.
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 B
Quick Check:
Linear probing = check next slot sequentially [OK]
Hint: Linear probing means try next slot one by one [OK]
Common Mistakes:
Confusing linear probing with chaining
Thinking linear probing replaces existing elements
Assuming table size doubles on collision
2. Which of the following is the correct way to calculate the next index in linear probing for a hash table of size tableSize when a collision occurs at index?
easy
A. nextIndex = (index * 2) % tableSize;
B. nextIndex = (index - 1) % tableSize;
C. nextIndex = (index + 1) % tableSize;
D. nextIndex = (index + 3) % tableSize;
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 C
Quick Check:
Linear probing next index = (index + 1) % size [OK]
Hint: Next index = (current index + 1) mod table size [OK]
Common Mistakes:
Multiplying index instead of adding
Subtracting instead of adding
Adding a number other than 1
3. Consider a hash table of size 5 using linear probing. The hash function is h(key) = key % 5. Insert keys in this order: 7, 12, 17. What will be the final state of the hash table (index: value)?
medium
A. {0: null, 1: null, 2: 7, 3: 12, 4: 17}
B. {0: 12, 1: 17, 2: 7, 3: null, 4: null}
C. {0: null, 1: 7, 2: null, 3: 12, 4: 17}
D. {0: null, 1: 7, 2: 17, 3: 12, 4: null}
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.
Keys collide at 2, placed at 2,3,4 sequentially [OK]
Hint: All keys hash to same index, place next free slot [OK]
Common Mistakes:
Placing all keys at index 2 ignoring collisions
Not wrapping around the table
Inserting keys in wrong order
4. The following C# code tries to insert keys into a hash table using linear probing. What is the bug?
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);
medium
A. The hash function is incorrect for linear probing.
B. The Insert method should use recursion instead of a loop.
C. The initial hash table values should be 0, not -1.
D. The code does not wrap index back to 0 when it reaches the end of the table.
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 D
Quick Check:
Linear probing requires modulo to wrap index [OK]
Hint: Always use modulo to wrap index in linear probing [OK]
Common Mistakes:
Forgetting modulo causes index out of range
Changing hash function unnecessarily
Thinking initial values must be zero
5. You have a hash table of size 7 using linear probing. The hash function is 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?
hard
A. 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.
B. Search will always find key 31 without issues; no fix needed.
C. Key 31 will be overwritten by key 24; fix by rehashing all keys after deletion.
D. The hash function is wrong; fix by changing it to key % 5.
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 A
Quick Check:
Deleted slots need special marker to continue search [OK]
Hint: Use 'deleted' marker, not empty slot, to avoid search errors [OK]
Common Mistakes:
Marking deleted slots as empty breaks search
Assuming search always finds keys after deletion
Changing hash function unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.