Hash collisions explained with a parking lot analogy
Picture a parking lot with numbered spots. Your hash function looks at your license plate and tells you which spot to park in.
A collision is when your assigned spot is already taken. The strategies are just different parking policies:
- External chaining: every spot has a little lane behind it, and cars line up in that lane. Nobody moves; lanes just get longer.
- Linear probing: spot taken? Try the next one, then the next. Easy, but cars bunch up into long clusters.
- Quadratic probing: jump 1, then 4, then 9 spots away. Breaks up clusters, but you might never try some spots.
- Double hashing: a second function on your plate decides how far you jump each time.
Resizing is building a bigger lot and re-parking every car, because each car's spot depends on the lot size. That's why load factor matters: once the lot is mostly full, finding a spot gets slow for everyone.
Comments
The parking lot analogy is perfect. Load factor makes so much more sense now.
Linear probing clusters = cars bunching up in one corner of the lot. I'll never forget that.