LearnThatStack Ace your next interview
Arrays, Strings & Hash Tables · question
Question 9 of 85

What is a hash collision and why do collisions happen?

beginner
← All Arrays, Strings & Hash Tables questions
Re-explain

A hash collision happens when two different keys land in the same bucket. It happens because a hash function squeezes a huge space of possible keys into a small, finite number of buckets.

There are far more possible keys than slots, so by the pigeonhole principle some keys must share a slot. Even a great hash function cannot avoid this. It can only make collisions rare and evenly spread. Poor functions make it worse by clustering keys together.

Collisions are normal, not a bug, so every hash table has a plan for them. Common approaches store the clashing entries in a small list per bucket, or probe for the next open slot. Handling them well is what keeps lookups near O(1) instead of degrading.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Arrays, Strings & Hash Tables One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. A hash collision is when two different keys end up in the same bucket of a hash table.
  2. It happens because the hash function maps a huge space of possible keys onto a small, fixed number of buckets.
  3. There are far more possible keys than slots, so by the pigeonhole principle some keys have to share one.
  4. Even a great hash function can't avoid that, but it keeps collisions rare and evenly spread, while a poor one clusters keys together.
  5. So collisions are normal, and every hash table plans for them, either with a small list per bucket or by probing for the next open slot.
  6. How well the table handles them decides whether lookups stay near O(1) or degrade as buckets fill up.

Weak answers to avoid

  • Treats collisions as a bug to eliminate With more possible keys than slots, no hash function for arbitrary keys can avoid them. Say they are normal and that every table has a plan for them.
  • Says what a collision is, never why The question asks why. Name the squeeze from a huge key space into a few buckets and the pigeonhole principle, not just two keys sharing a slot.
  • Confuses a collision with a duplicate key Inserting the same key twice updates one entry. A collision is two different keys sharing a bucket, and the table must keep both.
  • Stops at the definition, no handling or cost Mention per-bucket lists or probing for the next open slot, and that handling collisions well is what keeps lookups near O(1).
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Arrays, Strings & Hash Tables cheatsheet.

← Back to all Arrays, Strings & Hash Tables questions
Pro · $10/mo

74 of 85 Arrays, Strings & Hash Tables answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime