Data Structures and Algorithms
Keys 10, 17, 24 and 3 are stored in this order in a hash table of size 7 (positions 0 to 6) with the hash function h(k) = k mod 7. Collisions are resolved by linear probing, which moves forward one position at a time (wrapping from 6 to 0) to find a free slot. At which position is key 3 stored?
- APosition 3
- BPosition 4
- CPosition 6
Show answer and explanation
Answer: C. Position 6
All four keys leave a remainder of 3 when divided by 7, so each one first tries position 3. Key 10 goes into position 3; 17 finds position 3 taken and goes into position 4; 24 finds positions 3 and 4 taken and goes into position 5. Key 3 finds positions 3, 4 and 5 taken and is stored at position 6. As this shows, linear probing makes occupied positions bunch together (clustering), so keys that arrive later need more probes.
- A. This is the hash value itself, but 10 is already there.
- B. This is where 17 went. Position 5 after it is also taken by 24.
- C. Correct. Positions 3, 4 and 5 are taken, so it goes into the free position 6.