Where linear probing lands — Aptitude Question
Where linear probing lands: Keys are inserted into a hash table of size 10 with h(k) = k mod 10 and linear probing. Answer and worked solution on CodeKairo.
- Topic: Data Structures & Algorithms
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 75 seconds
Question
Keys are inserted into a hash table of size 10 with h(k) = k mod 10 and linear probing. The keys 12, 22 and 32 are inserted in that order into an empty table. At which index does 32 end up?
Options
- 2
- 3
- 4
- 5
Show the answer and the worked solution
Correct answer: C. 4
Worked solution
- 12 mod 10 = 2, so 12 takes index 2.
- 22 mod 10 = 2 as well; index 2 is occupied, so probing puts 22 at index 3.
- 32 mod 10 = 2; indices 2 and 3 are both taken, so probing continues to index 4, which is free.
Answer: 4.
The idea
Open addressing keeps every key inside the table, so collisions displace keys into neighbouring slots. Deletions then need tombstones, or the probe sequence for later keys breaks.
Shortcut. All three keys hash to 2, so they fill consecutive slots from 2 in insertion order, and the third lands at 2 + 2 = 4.
Common trap. 3 probes only one step past the home slot, forgetting that 22 already took index 3.
All Data Structures & Algorithms questions · placement mock tests