8.4 Double Hashing: Independent Step Probing & Coprime Design
π‘ Core Intuition: The 3-Second Mental Modelβ
π³ The Everyday Analogy:β
If two coworkers both get assigned desk #9 on Monday morning: under Linear Probing or Quadratic Probing they still walk down the exact same hallway path chasing open desks. Under Double Hashing, their employee badges hand each person a different personal stride size (+4 desks vs +7 desks) β so even when they collide at desk #9, they immediately scatter in completely independent directions!
π» Bridging to Computer Science:β
Double Hashing computes probe step size using a second independent hash function . Instead of limiting the table to fixed probe paths like Quadratic Probing, pairing yields up to distinct probe sequences β breaking both Primary and Secondary Clustering and closely approximating ideal Uniform Hashing.
πTable of Contents
β‘ Double Hashing Formula & Key-Dependent Stridesβ
Double Hashing selects the primary home bucket via and a key-specific stride via :
Notice what happens when two distinct keys 31 and 42 both collide with occupied home slot [9] (m = 11, , ):
π‘οΈ Two Strict Algebraic Design Invariants for Secondary Hash h2β
Breaking either invariant silently wedges insertions into tight infinite cycles even when the hash table is nearly empty:
- Non-Zero Stride Invariant ():
If for any key , then every single probe computes , trapping the probe on the same colliding slot forever!
- Prime Offset Formula: Pick prime and smaller prime : , guaranteed within .
- Full-Range Formula: Pick prime : , guaranteed within .
- Coprime Coverage Invariant (): To guarantee that stepping by visits every single table slot exactly once before cycling, and must share no common factor !
Safe Pairing Cheat Sheetβ
| Table Capacity Design () | Safe Formulation | Why Full Coverage Holds |
|---|---|---|
Prime (e.g., 11, 1009) | or | Any has automatically |
| Power of 2 () | Odd integer ( or g(k) | 1) | Only prime factor of is , so every odd stride is coprime |
| Composite (e.g., ) | Avoid unless explicitly | Factors like trap probes into partial sub-cycles |
π Real-World Production Contextβ
Double Hashing & Independent Probing in Modern Systems
Independent secondary strides minimize probe collisions where memory footprints stay tight
Kirsch-Mitzenmacher Bloom Filters
RocksDB / Edge Filters- Simulates k independent Bloom Filter bit positions cheaply from just TWO base hashes: g_i(x) = h1(x) + i * h2(x).
- Eliminates repeated expensive hashing per bit check across RocksDB SSTables & CDN edge caches.
SIMD SwissTable vs. Classic Double Hash
L1 Cache Trade-Off- Classical Double Hashing minimizes total probe hops at high alpha by scattering keys globally.
- Modern CPU maps (Abseil SwissTable) compare 16 header metadata bytes via SIMD before any random jump.
Consistent Ring Secondary Fallback
Distributed Sharding- When primary storage shard h1(key) experiences overload, secondary key-hashed strides spread fallback traffic across peers.
- Prevents hot-shard failover cascades from overwhelming the single adjacent node.
Embedded Fixed-Prime Hash Maps
Zero Dynamic Allocator- Kernel & firmware tables choose a prime bucket count m with 1 + (k mod (m-1)) to operate safely up to 80% full.
- Guarantees deterministic full-table reachability without heap fragmentation.
π Numerical Applications & Worked Examplesβ
- Consider a hash table of size () using Double Hashing with:
Insert the keys
[20, 31, 42, 108]in order and trace every probe slot . - A student designs a table of size using and . Give two distinct ways insertion fails on a key even when
10out of12slots remain empty!
Step-by-Step Solution:β
-
Tracing
[20, 31, 42, 108](m = 11):- Insert
20: (empty)T[9] = 20. - Insert
31: (collision!). Compute stride . Probe : (empty)T[2] = 31. - Insert
42: (same home slot as31!). Compute stride . Probe : (empty)T[5] = 42. - Insert
108: (collision!). Stride . Probe : (occupied by31!). Probe : (empty)T[6] = 108.
- Insert
-
Two Failure Modes for
m = 12, :- Zero Stride Hang (
k = 12): and . If slot[0]is busy, every probe computes , hanging forever! - Sub-Cycle Trap (
k = 16): and . Since , probes cycle strictly within{4, 8, 0}(4 -> 8 -> 0 -> 4...). If only those three slots are occupied (25%full!), insertion fails while9slots sit empty!
- Zero Stride Hang (
π― Exam & Interview Pitfall Checkβ
- How many distinct probe sequences can Double Hashing produce compared to Linear and Quadratic Probing?
- Linear and Quadratic probing produce at most distinct probe sequences (one per home slot). Double Hashing pairs independent and , yielding up to probe sequences!
- Can Double Hashing with power-of-two capacity ever satisfy the Coprime Coverage Invariant?
- Yes! Since has only
2as a prime factor, designing to always return an odd integer (g(k) | 1or2 * g(k) + 1) guarantees with zero modulo prime overhead!
- Yes! Since has only
- Does Double Hashing avoid Secondary Clustering completely?
- Mostly yes: Two colliding keys share yet diverge along different strides with probability , eliminating Secondary Clustering in practice.
- Writing Directly: Any key that is a multiple of yields , causing an immediate infinite self-collision loop on
h_1(k). Always ensure ! - Assuming Fewer Theoretical Probes Always Wins in Real RAM Benchmarks: Even though Double Hashing beats Linear Probing in probe hops above , Linear Probing checks adjacent slots inside one 64-byte L1 cache line while Double Hashing jumps across DRAM pages β always benchmark cache misses alongside probe counts!
- Forgetting to Rehash When Table Fills (): Even full-cycle Double Hashing visits slots in steps once empty slots vanish; double to the next safe prime (or ) well before !
π Navigation & Next Stepsβ
Next, we uncover why naive slot deletion breaks every Open Addressed table, how Tombstones (DELETED) and Backward-Shift Deletion rescue lookups, and derive CLRS probe complexity bounds in Topic 8.5 β Open Addressing: Tombstones & Probe Bounds.
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.