Skip to main content

8.4 Double Hashing: Independent Step Probing & Coprime Design

πŸ“šModule 08 β€’ Hashing & Collision ResolutionTopic 8.4⏱️12 min read
🎯High-Yield For:Semester Exams β€’ Technical Interviews β€’ Engineering Foundations

πŸ’‘ 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 h2(k)h_2(k). Instead of limiting the table to mm fixed probe paths like Quadratic Probing, pairing (h1(k),h2(k))(h_1(k), h_2(k)) yields up to Θ(m2)\Theta(m^2) distinct probe sequences β€” breaking both Primary and Secondary Clustering and closely approximating ideal Uniform Hashing.


⚑ Double Hashing Formula & Key-Dependent Strides​

Double Hashing selects the primary home bucket via h1(k)h_1(k) and a key-specific stride via h2(k)h_2(k):

h(k,i)=(h1(k)+iβ‹…h2(k))β€Šmodβ€Šm(i=0,1,…,mβˆ’1)h(k, i) = \bigl(h_1(k) + i \cdot h_2(k)\bigr) \bmod m \qquad (i = 0, 1, \dots, m - 1)

Notice what happens when two distinct keys 31 and 42 both collide with occupied home slot [9] (m = 11, h1(k)=kβ€Šmodβ€Š11h_1(k) = k \bmod 11, h2(k)=7βˆ’(kβ€Šmodβ€Š7)h_2(k) = 7 - (k \bmod 7)):


πŸ›‘οΈ 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:

  1. Non-Zero Stride Invariant (h2(k)β‰ 0h_2(k) \ne 0): If h2(k)=0h_2(k) = 0 for any key kk, then every single probe i=0,1,2,…i = 0, 1, 2, \dots computes h1(k)+iβ‹…0=h1(k)h_1(k) + i \cdot 0 = h_1(k), trapping the probe on the same colliding slot forever!
    • Prime Offset Formula: Pick prime mm and smaller prime R<mR < m: h2(k)=Rβˆ’(kβ€Šmodβ€ŠR)h_2(k) = R - (k \bmod R), guaranteed within [1,R][1, R].
    • Full-Range Formula: Pick prime mm: h2(k)=1+(kβ€Šmodβ€Š(mβˆ’1))h_2(k) = 1 + (k \bmod (m - 1)), guaranteed within [1,mβˆ’1][1, m - 1].
  2. Coprime Coverage Invariant (gcd⁑(h2(k),m)=1\gcd(h_2(k), m) = 1): To guarantee that stepping by +h2(k)β€Šmodβ€Šm+h_2(k) \bmod m visits every single table slot {0,1,…,mβˆ’1}\{0, 1, \dots, m - 1\} exactly once before cycling, h2(k)h_2(k) and mm must share no common factor >1> 1!

Safe Pairing Cheat Sheet​

Table Capacity Design (mm)Safe h2(k)h_2(k) FormulationWhy Full Coverage Holds
Prime mm (e.g., 11, 1009)1+(kβ€Šmodβ€Š(mβˆ’1))1 + (k \bmod (m - 1)) or Rβˆ’(kβ€Šmodβ€ŠR)R - (k \bmod R)Any 1≀h2(k)<m1 \le h_2(k) < m has gcd⁑(h2,m)=1\gcd(h_2, m) = 1 automatically
Power of 2 (m=2pm = 2^p)Odd integer (2β‹…g(k)+12 \cdot g(k) + 1 or g(k) | 1)Only prime factor of 2p2^p is 22, so every odd stride is coprime
Composite mm (e.g., m=12m = 12)Avoid unless gcd⁑(h2(k),m)=1\gcd(h_2(k), m) = 1 explicitlyFactors like 2,3,4,62, 3, 4, 6 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​

πŸ“Problem Statement: Multi-Key Double Hashing Trace & Partial-Cycle Failuretheorem
  1. Consider a hash table of size m=11m = 11 (0…100 \dots 10) using Double Hashing with: h1(k)=kβ€Šmodβ€Š11andh2(k)=7βˆ’(kβ€Šmodβ€Š7)h_1(k) = k \bmod 11 \qquad \text{and} \qquad h_2(k) = 7 - (k \bmod 7) Insert the keys [20, 31, 42, 108] in order and trace every probe slot i=0,1,2i = 0, 1, 2.
  2. A student designs a table of size m=12m = 12 using h1(k)=kβ€Šmodβ€Š12h_1(k) = k \bmod 12 and h2(k)=kβ€Šmodβ€Š6h_2(k) = k \bmod 6. Give two distinct ways insertion fails on a key even when 10 out of 12 slots remain empty!

Step-by-Step Solution:​

  1. Tracing [20, 31, 42, 108] (m = 11):

    • Insert 20: h1(20)=20β€Šmodβ€Š11=9h_1(20) = 20 \bmod 11 = 9 (empty) β€…β€ŠβŸΉβ€…β€Š\implies T[9] = 20.
    • Insert 31: h1(31)=31β€Šmodβ€Š11=9h_1(31) = 31 \bmod 11 = 9 (collision!). Compute stride h2(31)=7βˆ’(31β€Šmodβ€Š7)=7βˆ’3=4h_2(31) = 7 - (31 \bmod 7) = 7 - 3 = 4. Probe i=1i = 1: (9+1β‹…4)β€Šmodβ€Š11=2(9 + 1 \cdot 4) \bmod 11 = \mathbf{2} (empty) β€…β€ŠβŸΉβ€…β€Š\implies T[2] = 31.
    • Insert 42: h1(42)=42β€Šmodβ€Š11=9h_1(42) = 42 \bmod 11 = 9 (same home slot as 31!). Compute stride h2(42)=7βˆ’(42β€Šmodβ€Š7)=7βˆ’0=7h_2(42) = 7 - (42 \bmod 7) = 7 - 0 = 7. Probe i=1i = 1: (9+1β‹…7)β€Šmodβ€Š11=16β€Šmodβ€Š11=5(9 + 1 \cdot 7) \bmod 11 = 16 \bmod 11 = \mathbf{5} (empty) β€…β€ŠβŸΉβ€…β€Š\implies T[5] = 42.
    • Insert 108: h1(108)=108β€Šmodβ€Š11=9h_1(108) = 108 \bmod 11 = 9 (collision!). Stride h2(108)=7βˆ’(108β€Šmodβ€Š7)=7βˆ’3=4h_2(108) = 7 - (108 \bmod 7) = 7 - 3 = 4. Probe i=1i = 1: (9+4)β€Šmodβ€Š11=2(9 + 4) \bmod 11 = 2 (occupied by 31!). Probe i=2i = 2: (9+2β‹…4)β€Šmodβ€Š11=17β€Šmodβ€Š11=6(9 + 2 \cdot 4) \bmod 11 = 17 \bmod 11 = \mathbf{6} (empty) β€…β€ŠβŸΉβ€…β€Š\implies T[6] = 108.
  2. Two Failure Modes for m = 12, h2(k)=kβ€Šmodβ€Š6h_2(k) = k \bmod 6:

    • Zero Stride Hang (k = 12): h1(12)=0h_1(12) = 0 and h2(12)=12β€Šmodβ€Š6=0h_2(12) = 12 \bmod 6 = 0. If slot [0] is busy, every probe i=0,1,2,…i = 0, 1, 2, \dots computes (0+iβ‹…0)β€Šmodβ€Š12=0(0 + i \cdot 0) \bmod 12 = 0, hanging forever!
    • Sub-Cycle Trap (k = 16): h1(16)=4h_1(16) = 4 and h2(16)=16β€Šmodβ€Š6=4h_2(16) = 16 \bmod 6 = 4. Since gcd⁑(4,12)=4>1\gcd(4, 12) = 4 > 1, probes cycle strictly within {4, 8, 0} (4 -> 8 -> 0 -> 4...). If only those three slots are occupied (25% full!), insertion fails while 9 slots sit empty!

🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions
  1. How many distinct probe sequences can Double Hashing produce compared to Linear and Quadratic Probing?
    • Linear and Quadratic probing produce at most mm distinct probe sequences (one per home slot). Double Hashing pairs independent h1(k)h_1(k) and h2(k)h_2(k), yielding up to m(mβˆ’1)=Θ(m2)m(m - 1) = \Theta(m^2) probe sequences!
  2. Can Double Hashing with power-of-two capacity m=2pm = 2^p ever satisfy the Coprime Coverage Invariant?
    • Yes! Since 2p2^p has only 2 as a prime factor, designing h2(k)h_2(k) to always return an odd integer (g(k) | 1 or 2 * g(k) + 1) guarantees gcd⁑(h2(k),2p)=1\gcd(h_2(k), 2^p) = 1 with zero modulo prime overhead!
  3. Does Double Hashing avoid Secondary Clustering completely?
    • Mostly yes: Two colliding keys share h1(k1)=h1(k2)h_1(k_1) = h_1(k_2) yet diverge along different strides h2(k1)β‰ h2(k2)h_2(k_1) \ne h_2(k_2) with probability 1βˆ’1/m1 - 1/m, eliminating Secondary Clustering in practice.
Common Interview Traps: Architectural Trade-Offs
  • Writing h2(k)=kβ€Šmodβ€Šmh_2(k) = k \bmod m Directly: Any key that is a multiple of mm yields h2(k)=0h_2(k) = 0, causing an immediate infinite self-collision loop on h_1(k). Always ensure h2(k)β‰₯1h_2(k) \ge 1!
  • Assuming Fewer Theoretical Probes Always Wins in Real RAM Benchmarks: Even though Double Hashing beats Linear Probing in probe hops above Ξ±=0.6\alpha = 0.6, 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 (Ξ±β†’1.0\alpha \to 1.0): Even full-cycle Double Hashing visits mm slots in O(m)\mathcal{O}(m) steps once empty slots vanish; double mm to the next safe prime (or 2p2^p) well before Ξ±>0.75\alpha > 0.75!

πŸš€ 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.

πŸš€ Level Up Your Engineering Mindset

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
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Help keep Binary Dose free for students, or share your articles and notes.