Exam-style question
Try this first
A hash table uses the hash function h(key) = key modulo 7. The keys 10 and 17 are inserted. Explain what happens and describe how rehashing could be used to handle the result.
Model answer
What a good answer should say
- The key 10 produces 3 because 10 modulo 7 is 3.
- The key 17 also produces 3 because 17 modulo 7 is 3.
- Therefore, a collision occurs because both keys compute the same hash.
- Rehashing can be used by applying another hash calculation to one or both keys, such as h2(key) = (key + 1) modulo 7.
Explanation
Why this works
The answer identifies the equal hash values, defines the collision correctly and shows that rehashing must be checked because a second calculation can itself produce a collision.
Common mistake
No common mistake is linked to this question yet.
