Skip to main content

Command Palette

Search for a command to run...

πŸ—‚οΈ Hash Tables Explained Like You're 5

Published
β€’1 min read
S

Building AI systems and writing about how they actually work. Master of AI @ University of Technology Sydney. Previously B.Tech CS with focus on IoT. I believe the best way to learn is to explain. That's why I'm documenting tech concepts with simple analogies (@sreekarreddy.com). AWS Certified β€’ Azure AI Certified β€’ Neo4j Professional β€’ Google Data Analytics When not coding: exploring Sydney, working on side projects, and teaching tech to anyone who'll listen.

A library card catalog

Day 37 of 149

πŸ‘‰ Full deep-dive with code examples


The Library Catalog

Old libraries had card catalogs:

Looking for "Harry Potter"?

  • H β†’ Drawer for H
  • Find "Harry Potter" card
  • Card says: Shelf 3, Row 2

Jump directly to the right drawer!


How It Works

A hash function turns keys into locations:

"apple" β†’ hash β†’ 42
"banana" β†’ hash β†’ 17
"cherry" β†’ hash β†’ 8

Now you know where to look.


Why So Fast?

Array: Look through EVERY item = O(n) Hash Table: Jump directly to location = O(1)

Array: Check 1? No. Check 2? No. Check 3? Yes!
Hash Table: Calculate location β†’ Jump there β†’ Found!

The Catch: Collisions

What if two keys hash to the same spot?

"apple" β†’ 42 "pear" β†’ 42 (uh oh!)

Solutions:

  • Chain (link multiple items at same spot)
  • Probe (look at next spot)

In One Sentence

Hash tables use a function to instantly calculate where data is stored, making lookups super fast.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

More from this blog

esreekarreddy

132 posts