What is the main idea behind open addressing with linear probing in hash tables?
It means when a collision happens (two keys want the same spot), we look for the next empty spot in the table by moving one step at a time until we find a free slot.
Click to reveal answer
intermediate
In linear probing, what happens if the hash table is full or nearly full?
The search for an empty spot can take longer because many slots are occupied, causing clustering and slowing down insertions and lookups.
Click to reveal answer
beginner
Why do we use modulus (%) operation in linear probing?
To wrap around the hash table index when we reach the end, so we continue searching from the start of the table.
Click to reveal answer
intermediate
What is a primary disadvantage of linear probing compared to other collision handling methods?
It can cause clustering, where groups of filled slots build up, making collisions more frequent and slowing down operations.
Click to reveal answer
beginner
How does linear probing find a key during search in a hash table?
It starts at the hashed index and checks each slot moving forward one by one until it finds the key or an empty slot (meaning the key is not present).
Click to reveal answer
What does linear probing do when a collision occurs in a hash table?
ARehashes the key with a different function
BCreates a linked list at the collided slot
CDeletes the existing key to insert the new one
DChecks the next slot sequentially until an empty one is found
✗ Incorrect
Linear probing searches the next slots one by one to find an empty slot after a collision.
Which problem is commonly caused by linear probing in hash tables?
AClustering of filled slots
BMemory leaks
CStack overflow
DInfinite loops in all cases
✗ Incorrect
Linear probing can cause clustering, where many filled slots group together, slowing down operations.
How does linear probing handle the end of the hash table array during probing?
ASkips to the middle of the table
BStops searching and returns failure
CWraps around to the beginning using modulus operation
DDoubles the size of the table automatically
✗ Incorrect
Linear probing uses modulus to wrap around the array index to continue searching from the start.
When searching for a key using linear probing, what indicates the key is not in the table?
AFinding a slot with a different key
BEncountering an empty slot during the probe
CReaching the last slot in the table
DFinding a slot with the same key
✗ Incorrect
If an empty slot is found during probing, it means the key was never inserted.
What is the main operation performed repeatedly in linear probing after a collision?
AIncrementing the index by one and checking the slot
BRehashing the key with a new function
CRemoving the collided key
DSplitting the table into smaller parts
✗ Incorrect
Linear probing increments the index by one each time to find the next available slot.
Explain how linear probing handles collisions in a hash table and what happens when the end of the table is reached.
Think about walking forward in a circle looking for an empty seat.
You got /4 concepts.
Describe the main disadvantage of linear probing and how it affects hash table performance.
Imagine people sitting close together making it hard to find a free seat.
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 secondary hash function to find a new slot.
B. Use a linked list to store multiple values at the same index.
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 collision handling methods
Linear probing is a method where, upon collision, the algorithm checks the next slot in the array sequentially.
Step 2: Compare with other methods
Other methods include chaining (linked lists), resizing, or double hashing, but linear probing specifically uses sequential slots.
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 linear probing uses a second hash function
Assuming table size doubles on collision
2. Which of the following Rust code snippets correctly implements linear probing to find the next index after a collision in a hash table of size table_size?
easy
A. let next_index = (current_index + 1) % table_size;
B. let next_index = (current_index * 2) % table_size;
C. let next_index = (current_index - 1) % table_size;
D. let next_index = (current_index + 3) % table_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 each option
let next_index = (current_index + 1) % table_size; adds 1 and uses modulo, which is correct. Others multiply or subtract, which are not standard linear probing steps.
Final Answer:
let next_index = (current_index + 1) % table_size; -> Option A
Quick Check:
Linear probing = increment index by 1 modulo table size [OK]
Hint: Linear probing increments index by 1 modulo table size [OK]
Common Mistakes:
Using multiplication instead of addition
Subtracting index without handling negative wrap
Skipping modulo operation causing out-of-bounds
3. Consider the following Rust code snippet for inserting keys into a hash table using linear probing. What will be the final state of the hash table array after inserting keys [5, 15, 25] with table size 10 and hash function key % 10?
let mut table = vec![None; 10];
let keys = vec![5, 15, 25];
for &key in &keys {
let mut index = key % 10;
while table[index].is_some() {
index = (index + 1) % 10;
}
table[index] = Some(key);
}
println!("{:?}", table);
Hint: Keys with same hash go to next free slot sequentially [OK]
Common Mistakes:
Placing all keys at index 5 ignoring collisions
Skipping modulo wrap-around
Inserting keys at wrong indices after collision
4. The following Rust code attempts to insert keys into a hash table using linear probing. However, it sometimes causes an infinite loop. What is the bug?
let mut table = vec![None; 5];
let keys = vec![1, 6, 11];
for &key in &keys {
let mut index = key % 5;
while table[index].is_some() {
index = (index + 1) % 5;
}
table[index] = Some(key);
}
medium
A. The code does not check if the table is full, causing infinite loop when no empty slot exists.
B. The modulo operator is used incorrectly; it should be key % 6.
C. The loop should break after checking only once to avoid infinite loop.
D. The vector size is too large, causing index out of bounds.
Solution
Step 1: Analyze the while loop condition
The loop continues while the slot is occupied, moving to next index with wrap-around.
Step 2: Identify missing termination condition
If the table is full, the loop cycles endlessly because no empty slot is found and no exit condition exists.
Final Answer:
The code does not check if the table is full, causing infinite loop when no empty slot exists. -> Option A
Hint: Always check for full table to avoid infinite loops [OK]
Common Mistakes:
Ignoring full table condition
Changing modulo divisor incorrectly
Breaking loop too early causing wrong insertion
5. You have a hash table of size 7 using linear probing for collision handling. You insert keys [10, 20, 15, 7, 32] with hash function key % 7. After all insertions, what is the state of the table? Represent empty slots as null.
Explain your answer step-by-step.