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.
Top comments (0)