Hash Tables Explained: Hash Functions, Collisions, Probing and Load Factor
How hash tables store and find keys, how chaining, linear probing, quadratic probing and double hashing handle collisions, and why load factor matters.

Hash Tables Explained: Hash Functions, Collisions, Probing and Load Factor
Hash tables are behind Python dictionaries, JavaScript objects, database indexes and caches. They find a key in about one step on average, however many keys they hold. The idea is simple, but the details, especially collisions, are a favourite exam topic. This guide explains them with a worked example that you can reproduce in the free hash table simulator.
The basic idea
A hash function turns a key into a slot number. For whole-number keys and a table of size m, the simplest is
h(k) = k mod m
With m = 11, key 31 goes to slot 31 mod 11 = 9. To find 31 later, compute 9 again and look there.
Choosing a prime table size helps: with k mod m, keys that share a factor with m crowd into a few slots.
Collisions
Two keys can hash to the same slot: 22 mod 11 = 0 and 88 mod 11 = 0. There are two families of solutions.
Separate chaining
Each slot holds a list. Colliding keys are simply added to the list. Searching means hashing to the slot and scanning its list. The table never fills up, but long lists slow it down.
Open addressing
All keys live in the table itself. On a collision, follow a probe sequence until a free slot appears.
- Linear probing: try h, h + 1, h + 2, … (wrapping round). Simple, but keys form clusters, long runs of filled slots that make later insertions slower.
- Quadratic probing: try h, h + 1², h + 2², h + 3², … This breaks up clusters, but with a prime table size it only reaches about half the slots, so it can fail when the table is more than half full.
- Double hashing: try h, h + d, h + 2d, … where the step d comes from a second hash function, such as d = 1 + (k mod (m − 1)). Different keys take different steps, which spreads them best.
A worked example
Insert 10, 22, 31, 4, 15, 28, 17, 88, 59 into a table of size 11 using linear probing:
- 10 → 10, 22 → 0, 31 → 9, 4 → 4.
- 15 → 4 is taken, so try 5: store at 5.
- 28 → 6, 17 → 6 is taken, try 7: store at 7.
- 88 → 0 is taken, try 1: store at 1.
- 59 → 4, 5, 6 and 7 are taken, so it ends at 8.
Final table: 22, 88, –, –, 4, 15, 28, 17, 59, 31, 10.
Notice how 59 needed five probes because of the cluster from 4 to 7. With double hashing the same keys give 22, –, 59, 17, 4, 15, 28, 88, –, 31, 10, and the cluster is broken up. The simulator logs every probe so you can follow each decision.
Load factor
The load factor α = n ÷ m (keys per slot) predicts performance. For linear probing, a successful search takes about ½(1 + 1 ÷ (1 − α)) probes on average:
- α = 0.5: about 1.5 probes
- α = 0.75: about 2.5 probes
- α = 0.9: about 5.5 probes
Performance collapses as the table fills, so implementations resize before α gets too high, typically around 0.7 to 0.75: allocate a table about twice as large and reinsert every key, because each key's slot depends on m. Set a resize threshold in the simulator to watch this happen.
Deleting keys
In open addressing you cannot just empty a slot: a search for a key placed after probing past it would stop at the gap and wrongly report "not found". Instead mark the slot as deleted (a tombstone). Searches skip over tombstones, and new insertions may reuse them.
Exam checklist
- Show h(k) for each key.
- List the probes for every collision.
- Draw the final table with slot numbers.
- State the load factor at the end.
Limits
The simulator uses textbook hash functions and strategies. Real libraries use stronger hash functions, different growth policies and methods such as Robin Hood or cuckoo hashing. Table size is limited to 101 slots for clarity.


