A hash table has 11 locations, indexed 0 to 10. The hashing function is h(key) = key MOD 11. Collisions are handled by rehashing using linear probing (the next free location is used, wrapping round from 10 to 0).
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Key |
(a) Show the contents of the hash table after the keys 23, 45, 70, 13, 81 are inserted in that order. [3]
(b) Explain the steps taken to search the table for the key 92 and state how many occupied locations are examined before the search ends. [2]
(c) State what is meant by a collision in a hash table. [1]
- (a) 23 at index 1 and 70 at index 4(1)
- (a) 45 at index 2 and 13 at index 3(1)
- (a) 81 at index 5 (and no other locations used)(1)
- (b) Hash 92 to give 4; compare with key at 4 (70), then move on to 5 (81), then reach empty location 6 so the search stops – key not present (allow: search ends at the first empty location)(1)
- (b) 2 occupied locations examined(1)
- (c) Two (or more) different keys produce the same hash value / are mapped to the same location in the table(1)
- Answer: (a) 1: 23, 2: 45, 3: 13, 4: 70, 5: 81 (b) 92 MOD 11 = 4; check 4, then 5, then empty location 6 means not found – 2 occupied locations examined. (c) Two different keys hash to the same location.
- Examiner tip: When searching with linear probing, reaching an empty location is what proves the key isn't there – you don't search the whole table.