1. From key to index
A hash table is an array plus a function. The function takes a key and returns an integer bucket index. If the function is fast and spreads keys evenly, lookup stops being a search and becomes an array access. The hash function must be deterministic: the same key must always produce the same bucket.
Real hash functions mix the bits before taking the modulus. A poor function maps similar keys to similar buckets, which turns the table into a linked list. A good function is fast, deterministic, and uniform enough that the keys you actually store are spread out.
2. Collisions
Two different keys can hash to the same bucket. This is not a bug; it is guaranteed by the pigeonhole principle once the number of possible keys exceeds the number of buckets. The table needs a collision strategy, and the strategy is where most of the engineering lives.
- Chaining — each bucket holds a list (or tree) of entries. Lookup hashes to a bucket, then searches the chain.
- Open addressing — all entries live in the table. On a collision, probe the next slot (linear, quadratic, or double hashing) until an empty slot appears.
Example: h(k) = k mod 7
Insert 18, 25, 32, 41, 53, 60. The buckets are 4, 4, 4, 6, 4, 4. Five keys land in bucket 4 and one in bucket 6. With chaining, a lookup of 53 scans the bucket-4 chain; with linear probing, the keys spill into the next free slots.
3. Load factor
The load factor is the number of entries divided by the number of buckets. It is the single number that best predicts how much collision work a lookup will do. With chaining, the expected chain length is α; with open addressing, probes rise sharply as α approaches 1.
A typical chaining table resizes when α passes about 0.75 to 1. An open-addressing table often resizes earlier, around 0.5 to 0.7, because clustering makes probes expensive. The resize itself is O(n), but it is amortized over the insertions that triggered it.
4. Chaining vs probing
Chaining is easy to reason about and tolerates α > 1, but it allocates a node per entry and has poor locality. Open addressing stores everything in one array, which is cache-friendly, but deletions need tombstones and the table must keep empty slots.
- Chaining — simple, robust, extra memory; Java's HashMap uses it (and treeifies long chains).
- Linear probing — simple and fast, but primary clustering creates long runs of occupied slots.
- Double hashing — a second hash chooses the probe step, reducing clustering.
- Worst case — O(n) for both strategies if every key collides. Expected O(1) requires a good hash and a bounded load factor.
5. Practice
With h(k) = k mod 10, where do 23, 46, and 58 go?
With h(k) = k mod 7, which keys collide in the worked example?
Why does linear probing slow down as the table fills?
What is the worst-case lookup in a hash table, and when does it happen?
6. Resources
The next structure keeps the search decision instead of computing it: a tree. A binary search tree stores the comparison at every node, and its shape decides whether search is logarithmic or linear.