How fast is a hash map, really?
After this: Give the usual cost and the worst cost of each operation, and say what keeps lookups fast.
On this page · 6 sections · 4 checks
People often say that "hash maps are O(1)". Is that true?
If O(1) is new to you, here is the short version. O(1) means that the work stays the same when the data grows. O(n) means that the work grows at the same rate as the data, so twice as many pairs means twice as much work. Here n is the number of pairs in the map. The lesson Counting steps explains both in more detail.
The honest answer is that it is usually true, but not always. Interviewers often listen for this difference. This lesson shows when each answer holds.
The usual case and the worst case
Take six keys, inserted in this order: cat, dog, fox, emu, ant and owl. Put them in a table of eight slots, once with each of two hash functions.
- Sum of codes, the hash function from these lessons.
dogandowlshare slot 2, and the other four keys have a slot each. - Always 0, a very bad hash function that gives 0 for every key. All six keys land in slot 0, in one long chain.
Now count the comparisons. In the second table, owl is last in the chain of six.
| Sum of codes | Always 0 | |
|---|---|---|
| Longest chain | 2 | 6 |
Comparisons to find owl | 2 | 6 |
| Comparisons to find all six keys, one after another | 7 | 21 |
Now imagine a million keys. With a good hash function, and a table that grows, chains stay short, so a lookup still takes a comparison or two. The work does not grow, so that is O(1). With the bad hash function, a lookup might compare a million keys. The work grows with the data, so that is O(n).
These two situations have names. The is what usually happens. The is the most work an operation can ever need.
Watch the worst case
The visualizer below starts with the hash function that always gives 0. Insert the six keys and look one up. Then switch the hash back to sum of codes at the top of the card (this empties the table), repeat the inserts, and compare the comparison counter.
- Insert cat → 1The hash function here always gives 0.
- Insert dog → 2
- Insert fox → 3
- Insert emu → 4
- Insert ant → 5
- Insert owl → 6
- Look up owl
- 0—
- 1—
- 2—
- 3—
- 4—
- 5—
- 6—
- 7—
Start with the first task above, or try any key.
- count
- 0
- load
- 0.00
- compares
- 0
What keeps lookups fast
Two things keep the average case fast.
- The hash function spreads the keys across the slots.
- The table grows, so that the load factor stays low.
Take either of these away and the chains get longer, until a lookup is the same as searching a list.
The costs, one by one
Here are all the costs together. Space means how much memory the map uses.
| Operation | Average | Worst case | Why |
|---|---|---|---|
| Lookup | O(1) | O(n) | Average: a short chain. Worst: every pair in one chain. |
| Insert | amortized O(1) | O(n) | A resize now and then re-inserts every pair. |
| Update | O(1) | O(n) | A lookup, then the value is replaced. |
| Delete | O(1) | O(n) | A lookup, then the pair is removed. |
| Space | O(n) | O(n) | The pairs, plus some empty slots. |
Amortized O(1) is the idea from Growing the table: one insert can be expensive, but the average over many inserts stays small.
The O(n) worst case for an insert is what real libraries pay. Our simple put is slower in one case: if every key is in one chain, a resize walks that chain for each pair it re-inserts. Real libraries do not re-check keys while they resize.
Space is O(n) because the number of empty slots stays in proportion to the pairs while the table grows. There is one exception: this table never shrinks. If you add a million pairs and delete nearly all of them, the two million or so slots stay. So for space, n means the most pairs the map has held.
Check yourself
"Hash map operations are O(1)." True or false?
Which of these would make lookups O(n)?
The table has resized from 8 to 16 slots. What does the next lookup cost?
How much memory does a hash map holding n pairs use?
Connect
- Give the average, worst-case and amortized costs of a hash map's operations.
- Name the two things that keep lookups fast: spreading the keys and growing the table.
- Give a three-part answer to the interview question about lookup cost.