3.3.4 · D3Hashing

Worked examples — Open addressing — linear probing, quadratic probing, double hashing

3,705 words17 min readBack to topic

This is the worked-examples companion to the parent open-addressing note. The parent gave you the three probe formulas and one example each. Here we hunt down every case class open addressing can throw at you — every collision pattern, every degenerate table, every deletion trap — and work each one to the last arithmetic step.

Before we start, one reminder of the machinery we are about to use constantly:


The scenario matrix

Every problem this topic can throw at you is one of the case classes below. Read the matrix first as a checklist — then each worked example is tagged with the cell(s) it fills, and together they cover the whole grid. If your viewer does not render tables, the same list is spelled out beneath it.

# Case class What makes it special Example
A No collision home slot is empty, done Ex 1
B Single collision one probe needed Ex 1, 2
C Cluster crawl (linear) long contiguous run of full slots Ex 2
D Wrap-around probe passes slot and loops to Ex 3
E Quadratic escape jump skips past a cluster Ex 4
F Quadratic failure free slot exists but is unreachable Ex 5
G Double hashing, same home two keys, same , different Ex 6
H Double hashing, trap degenerate step size Ex 6
I Deletion + tombstone (search & reuse) search crosses / insert reuses a deleted slot Ex 7
J Load-factor / limiting value probe cost as Ex 8
K Word problem (real world) translate a story into probes Ex 9
L Exam twist count total probes / compare schemes Ex 10

Same list, plainly (in case the table above does not render): A = no collision; B = one collision; C = linear cluster crawl; D = wrap-around past the end; E = quadratic jump over a cluster; F = quadratic cannot reach a free slot; G = double hashing splits keys with the same home; H = the infinite-loop trap; I = deletion needs a tombstone (both for searching past it and for reusing it); J = load factor pushing probe cost to infinity; K = a real-world story problem; L = an exam-style "count and compare" twist.

We use one small table for most examples so you can see the state build up:

Why ? Because is prime, which (as the parent note stressed and Prime table sizes explains) is exactly the condition that makes quadratic and double hashing behave. We will break that rule on purpose in Ex 5 to watch it fail.


Warm-up: no collision & a first collision (cells A, B)


Cluster crawl in linear probing (cell C)

A cluster is a run of already-full slots with no gap between them. Look at figure s01: the blue slots form one such run, and the orange slot is a new key that hit inside the run and got dragged to its far end — extending the cluster by one. That growth is what we mean by "clustering is live": each new colliding key makes the run longer, so the next colliding key must crawl even further.

Figure s01 (below): linear probing on . Blue = keys already placed in slots ; the bracket marks the full contiguous cluster; the orange arrow shows key being dragged to slot at the cluster's end.

Figure — Open addressing — linear probing, quadratic probing, double hashing

Wrap-around across the array end (cell D)

What if the home slot is near the top of the array and the cluster runs off the end? The loops us back to slot . Never treat the array as having an "end" — it is a ring.

Figure s02 (below): the ring. Blue slots are full; the green arrow shows a new key with home probing wrapping back to slot ; the dashed line reminds you that "one past slot " is slot .

Figure — Open addressing — linear probing, quadratic probing, double hashing

Quadratic escape vs failure (cells E, F)

Quadratic probing changes the offset to : the steps are . Small still moves gently, but the jumps grow fast, letting a key leap over a cluster.

Figure s03 (below): quadratic escape. Blue slots are a full cluster. The gray arrow is the tiny step () landing on slot ; the green arc is the step () that vaults from home slot clear over slots into the green slot .

Figure — Open addressing — linear probing, quadratic probing, double hashing

Double hashing — same home, different paths (cells G, H)

Double hashing makes the step size itself depend on the key via a second hash . Two keys that collide at home now walk different routes.

We use on our table. Why the ""? So the step is never — a step of would probe the same slot forever (cell H, the trap). Why i.e. ? To keep in , and since is prime, every value is coprime to , guaranteeing the probe visits all slots.

Figure s04 (below): diverging paths. Gray slot is the shared home. The blue arc shows key (step ) landing in slot ; the orange arc shows key (step ) landing in slot — same home, different destinations, no secondary clustering.

Figure — Open addressing — linear probing, quadratic probing, double hashing

Deletion and the tombstone — search past it AND reuse it (cell I)


Load factor as a limiting value (cell J)


A real-world word problem (cell K)


Exam-style twist — count total probes & compare (cell L)


Recall Did every cell get covered?

A(Ex1,2,9) · B(Ex1,2) · C(Ex2,9,10) · D(Ex3,9) · E(Ex4) · F(Ex5) · G(Ex6) · H(Ex6) · I(Ex7) · J(Ex8) · K(Ex9) · L(Ex10). ✓ Full grid.

Total probes for Ex 10 under linear?
Total probes for Ex 10 under double hashing?
Reachable quadratic residues ?
only — 3 slots
Expected unsuccessful probes at ?
Where does the reused insert C land in Ex 7?
slot — it reclaims 's tombstone
Max probes before an insert gives up?
(it has then examined every slot)