Data Structures and Algorithms
Which is the correct description of a collision in a hash table?
- ABeing able to work out the original key from its hash value
- BDifferent keys producing the same hash value (storage position)
- CThe same key producing a different hash value each time the program runs
Show answer and explanation
Answer: B. Different keys producing the same hash value (storage position)
A hash table computes a storage position from the key with a hash function, allowing search, insertion and deletion in O(1) on average. Because there are far more possible keys than table slots, different keys landing on the same position (a collision) cannot be avoided. Collisions are therefore handled by methods such as chaining, which links the elements at the same position into a list, or open addressing, which looks for another free position. The same key must always produce the same hash value or lookups would fail, and whether the key can be recovered from the hash value is a property that matters for cryptographic use, not collisions.
- A. Not being able to recover the input (one-wayness) is a property of cryptographic hash functions, which is a separate matter from collisions.
- B. Correct. Different keys mapping to the same position is a collision, and it must always be handled somehow.
- C. If the same key gave different values, data could not be found, so hash functions are designed not to do this.