Skip to main content

8.3 Open Addressing: Linear & Quadratic Probing

๐Ÿ“šModule 08 โ€ข Hashing & Collision ResolutionTopic 8.3โฑ๏ธ15 min read
๐ŸŽฏHigh-Yield For:Semester Exams โ€ข Technical Interviews โ€ข Engineering Foundations

๐Ÿ’ก Core Intuition: The 3-Second Mental Modelโ€‹

๐Ÿณ The Everyday Analogy:โ€‹

Imagine pulling into a packed street-parking block. Spot #3 is taken, so under Linear Probing you crawl slowly one car-length forward checking #4, #5, #6 until you squeeze into the first open spot โ€” creating a giant bumper-to-bumper traffic jam behind #3! Under Quadratic Probing, if #3 is busy, you floor the gas and jump +1, then +4, then +9 blocks ahead to leap clear of the local jam.

๐Ÿ’ป Bridging to Computer Science:โ€‹

Open Addressing (or Closed Hashing) eliminates linked lists and heap pointers altogether: every element resides directly inside the flat table array T[0 .. m - 1]. Consequently, load factor strictly satisfies ฮฑ=n/mโ‰ค1\alpha = n / m \le 1. Upon collision at slot hโ€ฒ(k)h'(k), both insertion and lookup follow a deterministic probe sequence h(k,i)h(k, i) until hitting the key or an empty slot.


๐Ÿš— Linear Probing (f(i) = i) & The Primary Clustering Trapโ€‹

With Linear Probing, probe offset grows linearly by +1+1 per collision attempt (i=0,1,โ€ฆ,mโˆ’1i = 0, 1, \dots, m - 1):

h(k,i)=(hโ€ฒ(k)+i)โ€Šmodโ€Šmh(k, i) = \bigl(h'(k) + i\bigr) \bmod m

Why Cache Locality Loves It โ€” Yet Primary Clustering Ruins Itโ€‹

Because consecutive checks ([3], [4], [5], [6]) sit inside the exact same 64-byte CPU L1 cache line, Linear Probing blazes at low load factors (ฮฑโ‰ค0.4\alpha \le 0.4).

However, as keys settle, Linear Probing suffers severely from Primary Clustering: long contiguous runs of occupied slots act like magnets! A run of length LL captures keys landing anywhere in all LL slots plus slot before/after โ€” making long clusters grow faster and faster!


๐Ÿฆ˜ Quadratic Probing (f(i) = i^2) & Secondary Clusteringโ€‹

To scatter colliding keys away from tight neighborhoods, Quadratic Probing steps by squared offsets:

h(k,i)=(hโ€ฒ(k)+i2)โ€Šmodโ€Šm(i=0,1,2,โ€ฆ,mโˆ’1)h(k, i) = \bigl(h'(k) + i^2\bigr) \bmod m \qquad (i = 0, 1, 2, \dots, m - 1)

(More generally, h(k,i)=(hโ€ฒ(k)+c1i+c2i2)โ€Šmodโ€Šmh(k, i) = (h'(k) + c_1 i + c_2 i^2) \bmod m.)

Does Quadratic Probing Solve All Clustering?โ€‹

  1. Primary Clustering Eliminated: Contiguous runs dissolve because probes jump $+0, +1, +4, +9, +16 \dots$ over occupied blocks.
  2. Secondary Clustering Remains: Two distinct keys sharing the exact same home slot hโ€ฒ(k1)=hโ€ฒ(k2)h'(k_1) = h'(k_2) still walk through the identical quadratic probe path! Because there are only mm possible starting offsets, at most mm distinct probe sequences exist!
  3. Half-Table Guarantee (Critical Failure Risk!): Unlike Linear Probing (which visits all mm slots), Quadratic Probing (hโ€ฒ(k)+i2)โ€Šmodโ€Šm(h'(k) + i^2) \bmod m can loop forever without ever probing empty slots if mm isn't prime or ฮฑโ‰ฅ0.5\alpha \ge 0.5!
๐Ÿ“Quadratic Probing Prime Half-Table Theoremtheorem

If table size mm is an odd prime and the table is strictly less than half full (ฮฑ<0.5\alpha < 0.5, i.e., nโ‰คโŒŠm/2โŒ‹n \le \lfloor m / 2 \rfloor), then the first โŒˆm/2โŒ‰\lceil m / 2 \rceil quadratic probes h(k,0),h(k,1),โ€ฆ,h(k,โŒŠm/2โŒ‹)h(k, 0), h(k, 1), \dots, h(k, \lfloor m/2 \rfloor) are guaranteed to hit completely distinct table slots, so insertion always succeeds!


๐Ÿ“Š Linear vs. Quadratic Probing Comparisonโ€‹

Metric / PropertyLinear Probing (+i+i)Quadratic Probing (+i2+i^2)
Probe Step Pattern+0,+1,+2,+3,โ€ฆ+0, +1, +2, +3, \dots+0,+1,+4,+9,โ€ฆ+0, +1, +4, +9, \dots
CPU L1 Cache LocalityBest (contiguous cache-line reads)Moderate (small stride skips cache lines)
Primary ClusteringSevere (adjacent slots merge into mega-runs)Eliminated
Secondary ClusteringExists (part of Primary)Exists (identical hโ€ฒ(k)h'(k) shares exact path)
Full-Table Slot Coverage100% Guaranteed for any mmUp to โŒˆm/2โŒ‰\lceil m/2 \rceil slots (requires prime mm, ฮฑ<0.5\alpha < 0.5)

(Practical Fix: Choose m=2pm = 2^p paired with Triangular Probing f(i)=i(i+1)2โ€Šmodโ€Š2pf(i) = \frac{i(i+1)}{2} \bmod 2^p, which algebraically visits all 100%100\% of slots in a power-of-22 table!)


๐Ÿญ Real-World Production Context: CPython dict, SwissTable & Robin Hoodโ€‹

Open Addressing Engineering in Modern Runtimes

High-performance dictionaries combine contiguous inline tables with quadratic/triangular jumps and SIMD metadata

๐Ÿ

CPython 3.6+ Compact dict

Perturbed Probing + Dense Table
  • Separates a compact open-addressed index table from a dense insertion-ordered entry array.
  • Mixes higher hash bits (perturb >>= 5) into linear steps to crush Primary Clustering.
๐Ÿฆ€

Google SwissTable / Rust HashMap

SSE2/NEON 16-Byte SIMD + Triangular
  • Stores 1-byte H2 hash fingerprints per slot so SIMD instructions probe 16 contiguous slots in 1 clock cycle!
  • Jumps group-to-group via Triangular Probing i*(i+1)/2 mod m.
๐Ÿฆ

Robin Hood Hashing

Variance-Minimizing Linear Probing
  • Keeps Linear Probing's CPU cache friendliness while capping long probe chains.
  • During insert, a poor (far-from-home) key steals the slot of a rich (close-to-home) resident!
๐ŸŽฎ

Zero-Allocation Embedded Tables

Game Engines / HFT
  • Pre-allocates fixed stack/arena buffer with open addressingโ€”zero dynamic malloc/free latency jitter.
  • Powers real-time entity lookups under strict 16ms frame budgets.

๐Ÿ“ Numerical Applications & Worked Examplesโ€‹

๐Ÿ“Problem Statement: Linear vs. Quadratic Probe Tracetheorem

Using hash table size m=11m = 11 (0โ€ฆ100 \dots 10) and primary hash hโ€ฒ(k)=kโ€Šmodโ€Š11h'(k) = k \bmod 11, insert keys [22, 1, 13, 11, 24, 33] in exact order:

  1. Show the final slot placement under Linear Probing.
  2. Show the final slot placement under Quadratic Probing (f(i)=i2f(i) = i^2), marking total collisions encountered by 33.

Step-by-Step Solution:โ€‹

  1. Linear Probing Trace ((k + i) % 11):

    • 22 % 11 = 0 โ†’\to T[0] = 22
    • 1 % 11 = 1 โ†’\to T[1] = 1
    • 13 % 11 = 2 โ†’\to T[2] = 13
    • 11 % 11 = 0 โ†’\to Collides [0], [1], [2] โ†’\to T[3] = 11 (3 collisions)
    • 24 % 11 = 2 โ†’\to Collides [2], [3] โ†’\to T[4] = 24 (2 collisions)
    • 33 % 11 = 0 โ†’\to Collides [0], [1], [2], [3], [4] โ†’\to T[5] = 33 (5 collisions crawling through the merged primary cluster!)
  2. Quadratic Probing Trace ((k + i^2) % 11):

    • 22, 1, 13 land at [0], [1], [2].
    • 11: 0 (busy), 0 + 1 = 1 (busy), 0 + 4 = 4 (empty!) โ†’\to T[4] = 11.
    • 24: 2 (busy), 2 + 1 = 3 (empty!) โ†’\to T[3] = 24.
    • 33: 0 (busy), 0 + 1 = 1 (busy), 0 + 4 = 4 (busy), 0 + 9 = 9 (empty!) โ†’\to T[9] = 33 (3 collisions โ€” jumps completely clear of slots 2, 3, 5..8!).

๐ŸŽฏ Exam & Interview Pitfall Checkโ€‹

Core Conceptual Questions
  1. Can Open Addressing ever reach ฮฑ>1.0\alpha > 1.0?
    • Never! Every key occupies one slot inside the mm-slot array itself, so nโ‰คmโ€…โ€ŠโŸนโ€…โ€Šฮฑโ‰ค1.0n \le m \implies \alpha \le 1.0 is physically bounded.
  2. Why does Quadratic Probing (h'(k) + i^2) % m fail when m=10m = 10 and ฮฑ>0.5\alpha > 0.5?
    • Perfect squares modulo 1010 only produce {0, 1, 4, 5, 6, 9} โ€” four slots can never even be generated regardless of how empty the table is!
  3. Between Primary Clustering and Secondary Clustering, which grows worse as ฮฑโ†’0.9\alpha \to 0.9?
    • Primary Clustering (Linear Probing), because independent clusters merge into massive roadblocks capturing keys from all nearby buckets simultaneously!
Common Interview Traps: Architectural Trade-Offs
  • Writing i % m Loop Stopping Only on EMPTY Without Checking n == m: If an Open Addressed table actually hits 100% capacity (ฮฑ=1.0\alpha = 1.0) with zero EMPTY slots left, searching for a missing key spins in an infinite loop! Always bound loops to m probes maximum (and resize cleanly around ฮฑ=0.7\alpha = 0.7).
  • Confusing Primary vs. Secondary Clustering: Linear Probing suffers from both; Quadratic Probing cures Primary Clustering yet retains Secondary Clustering because h'(k1) == h'(k2) still forces identical probe offsets (+1, +4, +9).
  • Naive Deletion Breaking Searches: Simply setting T[pos] = EMPTY inside any Open Addressed table orphans keys further down the probe clusterโ€”Topics 8.4 and 8.5 complete Open Addressing using Double Hashing and Tombstones (DELETED)!

๐Ÿš€ Navigation & Next Stepsโ€‹

Next, we eliminate Secondary Clustering completely via key-dependent stride steps in Topic 8.4 โ€” Double Hashing: Independent Step Probing & Coprime Design.

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