Hash maps, visualized.
Turn the key into a number, and the number tells you which box to open. No searching. Watch keys land in buckets, collide and move when the map grows, then solve Two Sum with one.
What is a hash map?
A hash map stores key → value pairs, like a name and a phone number. It keeps an array of slots called buckets. To store a pair it runs the key through a hash function, which turns the key into a number, then takes the remainder by the number of buckets. That remainder is the bucket the pair lives in.
To find a key later, it does the same sum and jumps straight to that bucket, instead of looking through every pair. So put, get and delete cost O(1) on average, however many keys there are. Python’s dict and set are hash maps.
01 — one item first
Start with one put.
Eight buckets and one key. Watch the key become a number, the number pick a bucket, and get jump straight back to it.
Python
02 — the reason it exists
Hash map vs list: find by key.
The same eight pairs, stored two ways. A list has to check pair after pair. A hash map does the sum and opens one bucket.
A list of pairs
looks: 0
A hash map
looks: 0
Pick a key to look for.
03 — when two keys collide
Hash collisions and chaining.
Eight buckets and endless possible keys: some must share. Here a bucket holds a small linked list, the one from lesson 1, and a new key is pushed on the front, like the stack in lesson 2.
The map holds cat in bucket 0 and dog in bucket 2. Now put act.
04 — your turn
Put, get, delete.
Type a short key. Every button runs the class on the right, line by line, and tells you what it cost. Try owl or pig to make a collision.
Pick a key and press a button.
Python
05 — the classic bug
The get that never checked.
A tempting shortcut: jump to the bucket and return the first value there. It passes the obvious tests, where every key has a bucket to itself. When two keys share one, it hands back the wrong key’s value, with no error at all.
cat and act share bucket 0, with act in front. Ask for get("cat"), two ways.
Buggy
Fixed
06 — load factor
Load factor and resizing: when the buckets fill, double them.
The load factor is keys ÷ buckets. Past a limit (0.75 here; Python’s dict uses about 0.67), chains get long, so the map makes twice as many buckets and moves every key, because % 16 gives different answers from % 8.
8 buckets · bucket = hash % 8
16 buckets · bucket = hash % 16
Four keys in eight buckets. Keep adding.
07 — spot them in the wild
Hash map or not? Real examples.
A hash map is the answer whenever you look things up by an exact key. It is the wrong tool when you need order or “nearby”. Pick one for each, then read why.
Look up a phone number by name
Count how often each word appears
A leaderboard always sorted by score
Remove duplicate emails from a list
Find every name that starts with “Ra”
Remember the order things were added
Cache a slow function’s answer for each input
Find the smallest number, again and again
08 — the boss level
Two Sum with a hash map.
The most asked interview question there is: find two numbers that add up to a target. Checking every pair is O(n²). Instead, for each number ask the map one question: have I already seen the number that completes it?
seen = {number: index} · bucket = n % 8
Python
in seen is one jump. Checking every pair is O(n²): 4,950 pairs for 100 numbers. Memory O(n) for the map.09 — cheat sheet
Hash map time complexity.
Fast on average, slow only when keys pile into one bucket. In Python you rarely build one: dict, set, Counter and defaultdict are already hash maps.
| Job | Average | Worst | In Python |
|---|---|---|---|
| Put, get, delete by key | O(1) | O(n)every key in one bucket | d[k] = v, d[k], d.get(k), del d[k] |
| Is this key here? | O(1) | O(n) | k in d, k in my_set |
| Grow and rehash | O(1)spread over all puts (amortized) | O(n)the one put that triggers it | automatic |
| Count things | O(n)one pass | collections.Counter(words) | |
| Group things into lists | O(n)one pass | defaultdict(list), then groups[k].append(x) | |
| Visit every pair | O(n) | for k, v in d.items()in insertion order, since Python 3.7 | |
| Smallest key, sorted keys, keys near X | O(n) / O(n log n)hashing scatters order | min(d), sorted(d); a heap or sorted list does this better |
Two honest footnotes. Python’s dict handles collisions by probing for another free bucket (open addressing) rather than chaining, and it hashes strings with SipHash, which mixes every bit. The idea, and the costs, are the same.