Intuition What this page is
The parent note told you the rules . Here we play out every game the rules allow : every collision pattern, every degenerate table, every deletion trap, and the load-factor limits. If you meet a scenario in an exam that isn't on this page, we failed — so we enumerate them all first.
Definition Two symbols we lean on the whole page
Load factor α = m n = (number of stored keys n ) ÷ (capacity m ). It is the "how full is the table" number: α = 0.5 means half-full, α = 1 means packed. We use it from C2 onward.
Probe step f ( i ) : in open addressing we try slots slot i = ( h ( k ) + f ( i )) mod m for i = 0 , 1 , 2 , … . The shape of f is the whole difference between linear (f ( i ) = i ), quadratic (f ( i ) = i 2 ), and double hashing (f ( i ) = i ⋅ h 2 ( k ) ).
Every problem in this topic is one of these cells . We will hit each one with a worked example.
#
Case class
What makes it special
Example
C1
No collision
every key lands in an empty slot
Ex 1
C2
Chaining collision
multiple keys share a bucket
Ex 2
C3
Linear-probe collision
array-internal sliding
Ex 3
C4
Quadratic probing
jumps i 2 , may skip slots
Ex 4
C5
Double hashing
second hash sets the step
Ex 5
C6
Degenerate: all-collide
worst case, every strategy
Ex 6
C7
Deletion + tombstone
the severed-chain trap
Ex 7
C8
Limiting load factor
α → 1 , probe blow-up, rehash
Ex 8
C9
Word problem
real strings via polynomial hash
Ex 9
C10
Exam twist
quadratic probing fails to insert
Ex 10
Before we start, one symbol we reuse: h ( k ) = k mod m means "the remainder when k is divided by m ." Example: 22 mod 5 — how many left over after taking out fives? 22 = 4 × 5 + 2 , remainder = 2 . That remainder is always in { 0 , 1 , … , m − 1 } , exactly the legal slot range. That is why mod is the compression step: it forces any integer into a valid index.
Figure 1 below shows this compression visually: five big keys funnelling down to their five slots in a table of size m = 5 . Follow a coral arrow from a key to the slot its remainder picks — that arrow is the hash function.
Worked example Every key gets its own slot
Table size m = 7 . Insert keys 3 , 11 , 5 with h ( k ) = k mod 7 . Where do they land?
Forecast: guess the three slots before reading. Do any clash?
h ( 3 ) = 3 mod 7 = 3 → slot 3 empty → place. [_,_,_,3,_,_,_]
Why this step? 3 < 7 , so the remainder is just 3 itself.
h ( 11 ) = 11 mod 7 = 4 (since 11 = 1 × 7 + 4 ) → slot 4 empty → place. [_,_,_,3,11,_,_]
Why this step? 4 = 3 , so no clash with the previous key.
h ( 5 ) = 5 mod 7 = 5 → slot 5 empty → place. [_,_,_,3,11,5,_]
Verify: slots 3 , 4 , 5 are all distinct → zero probes, pure O ( 1 ) . This is the ideal every hash function chases. See Hash functions — properties and design for why good functions make C1 the common case.
Worked example Two keys, one bucket, a linked list
m = 6 , chaining. Insert 4 , 10 , 16 with h ( k ) = k mod 6 .
Forecast: these three keys differ by 6 each. What does that do?
h ( 4 ) = 4 mod 6 = 4 → bucket 4 list [4].
h ( 10 ) = 10 mod 6 = 4 (10 = 1 × 6 + 4 ) → collision → append → [4,10].
Why this step? Chaining never overwrites; it grows the linked list at that bucket.
h ( 16 ) = 16 mod 6 = 4 (16 = 2 × 6 + 4 ) → append → [4,10,16].
Verify: any key ≡ 4 ( mod 6 ) lands in bucket 4. Searching 16 scans 4→10→16: 3 comparisons . Expected cost is O ( 1 + α ) — recall α = n / m from the box above is the average list length, so a well-spread table keeps lists short; here the whole load piled into one bucket so it degraded — a preview of C6.
Worked example Sliding to the next free seat
m = 7 , linear probing with step f ( i ) = i , so slot i = ( h ( k ) + i ) mod 7 . Insert 10 , 17 , 24 .
Forecast: all three satisfy k mod 7 = 3 . Where does each end up?
h ( 10 ) = 10 mod 7 = 3 → slot 3 empty → place. [_,_,_,10,_,_,_]
h ( 17 ) = 17 mod 7 = 3 → slot 3 taken → i = 1 : ( 3 + f ( 1 )) mod 7 = ( 3 + 1 ) mod 7 = 4 empty → place.
Why this step? For linear probing f ( i ) = i , so f ( 1 ) = 1 — we slide exactly one slot to the right.
h ( 24 ) = 24 mod 7 = 3 → 3 taken, 4 taken → i = 2 : ( 3 + f ( 2 )) mod 7 = ( 3 + 2 ) mod 7 = 5 empty → place.
Final: [_,_,_,10,17,24,_] — a contiguous run of length 3.
Verify: search 24 probes slots 3 , 4 , 5 = 3 probes , exactly matching its insert path. That equality (search follows insert) is the invariant open addressing lives by.
Definition Primary clustering — what that run means
A primary cluster is a contiguous block of filled slots . Once it forms, any new key that hashes anywhere inside the block must probe all the way to the block's far end — so the cluster grows at both edges and lengthens everyone's future probes. That is why the length-3 run above is not just cosmetic: it is a cost multiplier. Figure 2 draws the run and the two curved arrows showing 17 and 24 sliding right off the base slot 3 — trace them to see the cluster assemble.
Worked example Jumping by squares
m = 7 , quadratic step f ( i ) = i 2 , so slot i = ( h ( k ) + i 2 ) mod 7 . Insert 10 , 17 , 24 (same collisions as C3).
Forecast: the step sizes are f ( i ) = i 2 = 0 , 1 , 4 , 9 , … . Compare landing spots to C3.
h ( 10 ) = 3 , i = 0 : ( 3 + 0 ) mod 7 = 3 empty → place.
h ( 17 ) = 3 , i = 0 taken → i = 1 : ( 3 + 1 ) mod 7 = 4 empty → place.
h ( 24 ) = 3 , i = 0 taken, i = 1 taken → i = 2 : ( 3 + 2 2 ) mod 7 = ( 3 + 4 ) mod 7 = 0 empty → place.
Why this step? f ( 2 ) = i 2 = 4 , so we leap over slots 5 and 6 to wrap around to 0.
Final: [24,_,_,10,17,_,_] — note 24 is not adjacent to its cluster; that scattering is exactly how quadratic probing breaks up the primary clustering defined in C3.
Verify: search 24 probes 3 , 4 , 0 = 3 probes . Different slots than C3 but same probe count here. The scattering is the payoff.
Worked example A second hash picks the step
m = 7 . h 1 ( k ) = k mod 7 , h 2 ( k ) = 1 + ( k mod 5 ) . Step f ( i ) = i ⋅ h 2 ( k ) , so slot i = ( h 1 ( k ) + i ⋅ h 2 ( k )) mod 7 . Insert 10 , 17 .
Forecast: what step size does 17 use, and why must h 2 never be 0 ?
h 1 ( 10 ) = 3 , i = 0 → slot 3 empty → place.
h 1 ( 17 ) = 3 taken. Compute step h 2 ( 17 ) = 1 + ( 17 mod 5 ) = 1 + 2 = 3 .
Why 1 + … ? If h 2 could return 0 , the step would be 0 and we'd probe the same slot forever . Adding 1 guarantees a nonzero move.
i = 1 : ( 3 + 1 × 3 ) mod 7 = 6 empty → place.
Final: [_,_,_,10,_,_,17].
Verify: search 17 probes 3 (that's 10) then 6 (found) = 2 probes . Different keys get different step sizes → best spread, per the parent's table.
Worked example The worst case, every strategy
m = 5 . Insert 5 , 10 , 15 , 20 — all satisfy k mod 5 = 0 . We run all four resolution rules so no edge case is skipped.
Forecast: what is the search cost for the last key inserted, in each strategy?
Chaining: bucket 0 becomes [5,10,15,20]. Searching 20 scans all 4 nodes → O ( n ) .
Linear probing (f ( i ) = i ; n = 4 < m = 5 so it fits):
5 → slot 0. 2. 10 → 0 taken → slot 1. 3. 15 → slots 2. 4. 20 → slot 3. → [5,10,15,20,_]. Search 20 probes 0 , 1 , 2 , 3 = 4 probes .
Quadratic probing (f ( i ) = i 2 , base 0):
5 , i = 0 → 0. 2. 10 , i = 1 → ( 0 + 1 ) mod 5 = 1 . 3. 15 : i = 1 taken, i = 2 → ( 0 + 4 ) mod 5 = 4 . 4. 20 : i = 1 , 2 taken, i = 3 → ( 0 + 9 ) mod 5 = 4 taken, i = 4 → ( 0 + 16 ) mod 5 = 1 taken… the reachable set from base 0 is { 0 , 1 , 4 } — only 3 distinct slots, so 20 can never be placed even though slots 2,3 are empty. (Same failure family as C10.)
Double hashing (h 2 ( k ) = 1 + ( k mod 4 ) , step = i ⋅ h 2 ): h 2 ( 20 ) = 1 + ( 20 mod 4 ) = 1 , so 20 walks 0 , 1 , 2 , 3 and lands in slot 2 — 4 probes to place. Even distinct step sizes cannot beat O ( n ) when every key shares the base.
Verify: chaining and linear both cost 4 for the last key; quadratic reaches only { 0 , 1 , 4 } and fails to insert; double hashing also degrades to a length-4 walk. All four collapse to linear (or fail) under an adversarial hash — that is why the hash function quality matters more than the resolution strategy.
Worked example Why you can't just blank a slot
From C3's table [_,_,_,10,17,24,_] (linear probing, m = 7 ). Delete 17 , then search 24 .
Forecast: if we set slot 4 to empty, what happens to the search for 24?
Naive delete: blank slot 4 → [_,_,_,10,_,24,_].
Search 24 : start h ( 24 ) = 3 → slot 3 holds 10, not 24, keep probing → slot 4 is empty → search concludes "24 not present." Wrong! 24 is right there in slot 5.
Why this breaks: an empty slot is the universal "stop" signal; blanking slot 4 severed the probe chain.
Correct delete: write a tombstone X in slot 4 → [_,_,_,10,X,24,_].
Re-search 24 : slot 3 (10, no), slot 4 (tombstone → "keep going"), slot 5 (24 ✓).
Verify: with the tombstone, search 24 takes 3 probes and succeeds. Insert may later reuse the X. This is the exact trap flagged in the parent's [!mistake].
1 − α 1 explode, then rehash
Uniform-hashing open addressing. Compute expected unsuccessful-search probes at α = 0.5 , 0.9 , 0.95 . Then a table with m = 10 , n = 7 crosses the rehash threshold — what happens?
Forecast: guess the probe count at α = 0.9 before computing.
Formula (from parent): E = 1 − α 1 , where α = n / m is the load factor from the top box.
α = 0.5 : 1 − 0.5 1 = 2 probes.
α = 0.9 : 1 − 0.9 1 = 10 probes.
α = 0.95 : 1 − 0.95 1 = 20 probes — halving the gap to 1 doubles the pain.
Why this shape? Each probe hits a full slot with probability ≈ α ; the tries form a geometric series summing to 1 − α 1 .
Why threshold ≈ 0.7 ? Read E off the curve: at α = 0.7 , E = 1 − 0.7 1 ≈ 3.33 probes — still cheap. Just past it, cost climbs steeply (0.8 → 5 , 0.9 → 10 ). So 0.7 is the last point where the table is still fast ; we resize before the curve's knee. It is a chosen safety margin, not a law.
Rehash trigger: α = n / m = 7/10 = 0.7 hits the threshold → allocate m ′ = 20 , reinsert all 7 keys → new α = 7/20 = 0.35 .
Verify: new load factor 0.35 ⇒ expected probes 1 − 0.35 1 ≈ 1.54 — back near O ( 1 ) . Figure 3 plots the whole 1 − α 1 curve with the three marked points and the dashed 0.7 threshold line so you can see the knee. See Load factor and dynamic resizing / rehashing and Amortized analysis for why doubling keeps inserts O ( 1 ) amortized.
"ab" and "ba" with a polynomial hash
m = 13 , p = 31 , ASCII a = 97 , b = 98 . Polynomial rolling hash h code ( s ) = ∑ s i p L − 1 − i , then index = h code mod 13 .
Forecast: will "ab" and "ba" collide? (Their character sum is equal — beware.)
h code ( "ab" ) = 97 ⋅ 31 + 98 = 3007 + 98 = 3105 .
Index of "ab": 3105 mod 13 . 13 × 238 = 3094 , remainder 3105 − 3094 = 11 → slot 11.
h code ( "ba" ) = 98 ⋅ 31 + 97 = 3038 + 97 = 3135 .
Index of "ba": 3135 mod 13 . 13 × 241 = 3133 , remainder 3135 − 3133 = 2 → slot 2.
Why they differ: the polynomial weights position (base-p digits), so swapping letters changes the code — unlike a plain sum.
Verify: slot 11 = slot 2 : no collision, anagrams separated. See Polynomial rolling hash & Rabin–Karp and Sets and Maps / Dictionaries for where this powers real string keys.
i 2 can't find a free slot even though one exists
m = 6 (deliberately not prime ), quadratic step f ( i ) = i 2 , so slot i = ( h ( k ) + i 2 ) mod 6 . Slots 0 and 3 are free; everything else full. Insert a key with h ( k ) = 0 .
Forecast: the reachable slots from base 0 are 0 + i 2 mod 6 . List them.
i = 0 : ( 0 + 0 ) mod 6 = 0 — but slot 0 is full (say occupied by an earlier key we can't reuse).
i = 1 : ( 0 + 1 ) mod 6 = 1 — full.
i = 2 : ( 0 + 4 ) mod 6 = 4 — full.
i = 3 : ( 0 + 9 ) mod 6 = 3 — free! ... but watch the pattern continue.
i = 4 : ( 0 + 16 ) mod 6 = 4 ; i = 5 : ( 0 + 25 ) mod 6 = 1 . The reachable set is { 0 , 1 , 4 , 3 } — it cycles and never visits slot 2 or 5.
Why this fails in general: with m not prime (or α too high), quadratic probing reaches only a subset of slots, so it can report "full" while empties exist.
Verify: distinct reachable slots = { 0 , 1 , 3 , 4 } , exactly 4 of the 6 — slots 2 , 5 unreachable from base 0. This is the classic exam trap; the fix is m prime and α ≤ 0.5 .
Recall Self-test across the matrix
Which cell does each belong to?
Insert 3,11,5 into m = 7 , all distinct slots ::: C1 (no collision)
Bucket 4 becomes [4,10,16] ::: C2 (chaining collision)
Blanking a slot severs a probe chain ::: C7 (tombstone)
1 − 0.9 1 = 10 probes ::: C8 (limiting load factor)
"ab"→slot 11, "ba"→slot 2, m = 13 ::: C9 (word problem)
Quadratic reaches only { 0 , 1 , 3 , 4 } in m = 6 ::: C10 (exam twist)
Mnemonic Order of attack for any hashing problem
"Base, then Bump." First compute the base index h ( k ) mod m . Only if it clashes do you bump along your strategy's rule (chain / + i / + i 2 / + i ⋅ h 2 ).