8.3 Open Addressing: Linear & Quadratic Probing
๐ก 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 . Upon collision at slot , both insertion and lookup follow a deterministic probe sequence until hitting the key or an empty slot.
๐Table of Contents
- ๐ Linear Probing (f(i) = i) & The Primary Clustering Trap
- ๐ฆ Quadratic Probing (f(i) = i^2) & Secondary Clustering
- ๐ Linear vs. Quadratic Probing Comparison
- ๐ญ Real-World Production Context: CPython dict, SwissTable & Robin Hood
- ๐ Numerical Applications & Worked Examples
- ๐ฏ Exam & Interview Pitfall Check
- ๐ Navigation & Next Steps
๐ Linear Probing (f(i) = i) & The Primary Clustering Trapโ
With Linear Probing, probe offset grows linearly by per collision attempt ():
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 ().
However, as keys settle, Linear Probing suffers severely from Primary Clustering: long contiguous runs of occupied slots act like magnets! A run of length captures keys landing anywhere in all 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:
(More generally, .)
Does Quadratic Probing Solve All Clustering?โ
- Primary Clustering Eliminated: Contiguous runs dissolve because probes jump
$+0, +1, +4, +9, +16 \dots$over occupied blocks. - Secondary Clustering Remains: Two distinct keys sharing the exact same home slot still walk through the identical quadratic probe path! Because there are only possible starting offsets, at most distinct probe sequences exist!
- Half-Table Guarantee (Critical Failure Risk!): Unlike Linear Probing (which visits all slots), Quadratic Probing can loop forever without ever probing empty slots if isn't prime or !
If table size is an odd prime and the table is strictly less than half full (, i.e., ), then the first quadratic probes are guaranteed to hit completely distinct table slots, so insertion always succeeds!
๐ Linear vs. Quadratic Probing Comparisonโ
| Metric / Property | Linear Probing () | Quadratic Probing () |
|---|---|---|
| Probe Step Pattern | ||
| CPU L1 Cache Locality | Best (contiguous cache-line reads) | Moderate (small stride skips cache lines) |
| Primary Clustering | Severe (adjacent slots merge into mega-runs) | Eliminated |
| Secondary Clustering | Exists (part of Primary) | Exists (identical shares exact path) |
| Full-Table Slot Coverage | 100% Guaranteed for any | Up to slots (requires prime , ) |
(Practical Fix: Choose paired with Triangular Probing , which algebraically visits all of slots in a power-of- 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โ
Using hash table size () and primary hash , insert keys [22, 1, 13, 11, 24, 33] in exact order:
- Show the final slot placement under Linear Probing.
- Show the final slot placement under Quadratic Probing (), marking total collisions encountered by
33.
Step-by-Step Solution:โ
-
Linear Probing Trace (
(k + i) % 11):22 % 11 = 0T[0] = 221 % 11 = 1T[1] = 113 % 11 = 2T[2] = 1311 % 11 = 0Collides[0], [1], [2]T[3] = 11(3 collisions)24 % 11 = 2Collides[2], [3]T[4] = 24(2 collisions)33 % 11 = 0Collides[0], [1], [2], [3], [4]T[5] = 33(5 collisions crawling through the merged primary cluster!)
-
Quadratic Probing Trace (
(k + i^2) % 11):22, 1, 13land at[0], [1], [2].11:0(busy),0 + 1 = 1(busy),0 + 4 = 4(empty!)T[4] = 11.24:2(busy),2 + 1 = 3(empty!)T[3] = 24.33:0(busy),0 + 1 = 1(busy),0 + 4 = 4(busy),0 + 9 = 9(empty!)T[9] = 33(3 collisions โ jumps completely clear of slots2, 3, 5..8!).
๐ฏ Exam & Interview Pitfall Checkโ
- Can Open Addressing ever reach ?
- Never! Every key occupies one slot inside the -slot array itself, so is physically bounded.
- Why does Quadratic Probing
(h'(k) + i^2) % mfail when and ?- Perfect squares modulo only produce
{0, 1, 4, 5, 6, 9}โ four slots can never even be generated regardless of how empty the table is!
- Perfect squares modulo only produce
- Between Primary Clustering and Secondary Clustering, which grows worse as ?
- Primary Clustering (Linear Probing), because independent clusters merge into massive roadblocks capturing keys from all nearby buckets simultaneously!
- Writing
i % mLoop Stopping Only onEMPTYWithout Checkingn == m: If an Open Addressed table actually hits 100% capacity () with zeroEMPTYslots left, searching for a missing key spins in an infinite loop! Always bound loops tomprobes maximum (and resize cleanly around ). - 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] = EMPTYinside any Open Addressed table orphans keys further down the probe clusterโTopics8.4and8.5complete 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.
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.