Home › Interview Questions › What happens if two different records produce the …

What happens if two different records produce the same hash value? How should hash collisions be handled in a database?

🟡 Medium Conceptual Junior level
1Times asked
Nov 2026Last seen
Nov 2026First seen

💡 Model Answer

Hash collisions occur when two distinct inputs produce the same hash value. In a database, if you rely solely on the hash for uniqueness or as a primary key, a collision can cause duplicate records to be treated as the same row, leading to data loss or incorrect joins. The probability of a collision depends on the hash function and the size of the hash space; MD5 produces a 128‑bit value, which makes collisions extremely unlikely in practice, but not impossible. To handle collisions, you can: 1) Use a longer hash algorithm (e.g., SHA‑256) to reduce collision probability. 2) Store the original data alongside the hash and perform a secondary equality check when a hash match is found. 3) Add a unique constraint on the original columns to enforce uniqueness at the database level. 4) If collisions are detected, rehash with a different salt or algorithm. In most applications, the risk is negligible, but for critical systems you should implement a collision‑handling strategy to maintain data integrity.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500