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)