How does double hashing choose the probe step, and what conditions must the second hash function meet?
A second hash function d(k) gives each key its own step: probe h(k), h(k)+d(k), h(k)+2·d(k), … mod N. d(k) must never be 0, and N should be prime so the sequence can reach every cell.
With linear probing all colliding keys take step 1, so they line up behind each other. Quadratic probing jumps away, but has to compute a new jump distance after every collision. Double hashing is the compromise: d(k) is computed once per operation, and every further probe just adds the same step. Each key gets its own step size, so keys that collide at their first cell usually go separate ways afterwards. This avoids the clusters of linear probing.
The textbook choice is d(k) = q − (k mod q) for a prime q < N. Two conditions matter:
- d(k) ≠ 0. A step of 0 would probe the same full cell forever. q − (k mod q) is always between 1 and q, so it is safe.
- d(k) and N share no common factor. Otherwise the probe sequence cycles through only part of the table and may miss every free cell. Choosing N prime guarantees this for any step from 1 to N−1.
Double hashing costs one extra hash computation when the first cell is taken, and its probes jump around the array (worse for caches), but its expected probe count is close to the ideal $1/(1-α)$.
Go deeper:
Double hashing — the probe formula, why the step must be coprime to the table size, and how it compares with linear and quadratic probing.