A hash table contains 10 buckets and uses linear probing to resolve…
2005
A hash table contains 10 buckets and uses linear probing to resolve collisions. The key values are integers and the hash function used is key % 10. If the values 43, 165, 62, 123, 142 are inserted in the table, in what location would the key value 142 be inserted?
- A.
2
- B.
3
- C.
4
- D.
6
Attempted by 184 students.
Show answer & explanation
Correct answer: D
Solution: Use the hash function hash(key) = key % 10 and linear probing for collisions.
Insert 43: 43 % 10 = 3 → index 3 is free, place 43 at index 3.
Insert 165: 165 % 10 = 5 → index 5 is free, place 165 at index 5.
Insert 62: 62 % 10 = 2 → index 2 is free, place 62 at index 2.
Insert 123: 123 % 10 = 3 → index 3 is occupied (43), linear probe to index 4 which is free, place 123 at index 4.
Insert 142: 142 % 10 = 2 → index 2 is occupied (62). Probe to index 3 (occupied by 43), then index 4 (occupied by 123), then index 5 (occupied by 165). The next slot, index 6, is free, so place 142 at index 6.
Answer: The key 142 is inserted at index 6.
A video solution is available for this question — log in and enroll to watch it.