3.3.6Hashing

Load factor — when to resize, rehashing cost

2,130 words10 min readdifficulty · medium6 backlinks

WHAT is the load factor?

The load factor is the single number that controls how fast a hash table is. Small α\alpha = fast but wastes memory. Large α\alpha = saves memory but slows down.


WHY does α\alpha control speed?

Chaining: expected work per operation

Assume simple uniform hashing (each key equally likely to land in any bucket, independently). The expected length of the chain you must scan is the average chain length, which is exactly α\alpha.

E[cost of search]=1hash + index+αscan chain=O(1+α)\mathbb{E}[\text{cost of search}] = \underbrace{1}_{\text{hash + index}} + \underbrace{\alpha}_{\text{scan chain}} = O(1 + \alpha)

Why this step? Each of the nn keys is in some bucket. Probability a given key shares your bucket is 1/m1/m. Expected number sharing = (n)1m=α(n)\cdot\frac{1}{m} = \alpha. So the chain you walk has expected length α\alpha.

➡️ If we keep α=O(1)\alpha = O(1) (a small constant, say 0.75\le 0.75), then O(1+α)=O(1)O(1+\alpha) = O(1). This is the whole reason we resize.

Open addressing: expected probes

With uniform hashing and α<1\alpha < 1, the expected number of probes for an unsuccessful search is derived from a geometric-style argument:

E[probes]11α\mathbb{E}[\text{probes}] \le \frac{1}{1-\alpha}


Deriving 1/(1α)1/(1-\alpha) from scratch (open addressing)


HOW resizing works (and why it's expensive then cheap)

A single rehash of nn elements costs Θ(n)\Theta(n). That feels scary — one insert occasionally takes linear time! So why is the table still considered O(1)O(1) per operation?

Figure — Load factor — when to resize, rehashing cost

Amortized cost — the doubling trick, derived


Putting it together: the resize rule



Flashcards

What is the load factor of a hash table?
α=n/m\alpha = n/m, the ratio of stored keys nn to buckets mm.
Expected search cost in a chained hash table (uniform hashing)?
O(1+α)O(1+\alpha).
Expected probes for an unsuccessful search in open addressing?
11α\le \dfrac{1}{1-\alpha}.
Why does open addressing resize earlier (~0.7) than chaining (~1.0)?
Because 1/(1α)1/(1-\alpha) blows up sharply as α1\alpha\to1, whereas chaining cost grows only linearly as 1+α1+\alpha.
Cost of a single rehash of nn elements?
Θ(n)\Theta(n) — every key must be re-inserted modulo the new size.
Why can't we just copy buckets during a resize?
Bucket index is h(k)modmh(k)\bmod m; changing mm changes the index, so keys must be re-placed.
Amortized cost per insert with table doubling?
O(1)O(1); total move work over nn inserts is 2n\le 2n.
Why does additive growth (e.g. +100) give bad performance?
Rehashes every constant interval → total work Θ(n2)\Theta(n^2) → amortized O(n)O(n) per insert.
Geometric series behind doubling's amortized bound?
1+2+4++n=2n11+2+4+\dots+n = 2n-1.
Why use separated grow/shrink thresholds (hysteresis)?
To prevent grow↔shrink thrashing when ops alternate near a single boundary.
If you expect 1500 keys at α0.75\alpha\le0.75, minimum mm?
m1500/0.75=2000m \ge 1500/0.75 = 2000.

Recall Feynman: explain it to a 12-year-old

Imagine a coat-check room with hooks. The "load factor" is how many coats per hook. With few coats it's instant — one hook, one coat. As it fills, several coats hang on the same hook and you must flip through them, getting slower. So when the room is about three-quarters full, you move everything to a room with twice as many hooks. Moving everyone is a big chore — but you only do it occasionally, and because each new room is double the last, you almost never do it. Spread over all the coats you ever hang, that chore costs almost nothing per coat. That "almost nothing on average" is what we call amortized O(1)O(1).


Connections

  • Hashing — parent concept; hashing is fast only when α\alpha stays bounded.
  • Separate Chaining — tolerates α1\alpha \gtrsim 1; cost O(1+α)O(1+\alpha).
  • Open Addressing — needs α<1\alpha < 1; cost 1/(1α)1/(1-\alpha).
  • Amortized Analysis — the aggregate/accounting method proving O(1)O(1) inserts.
  • Dynamic Arrays — same doubling trick; ArrayList/vector grow identically.
  • Hash Functions — uniform hashing assumption underpins all the α\alpha formulas.

Concept Map

measures fullness

measures fullness

higher a increases

avg chain length a gives

geometric sum gives

degrades

degrades

keep a small triggers

explodes near 0.7 triggers

requires

cost spread over inserts

Load factor a = n/m

Chaining

Open addressing

Collisions

Search cost O(1+a)

Expected probes 1/(1-a)

Resize / grow table

Rehashing: move all keys

Amortized O(1)

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, load factor α=n/m\alpha = n/m ka matlab hai table kitna bhara hua hai — nn keys aur mm buckets. Jab table khaali hota hai, hashing ekdum fast hota hai, O(1)O(1). Lekin jaise-jaise keys badhti hain, collisions badhte hain aur har operation slow hone lagta hai. Chaining mein cost 1+α1+\alpha hoti hai (linear, gentle), par open addressing mein cost 11α\frac{1}{1-\alpha} hoti hai — yeh α=0.9\alpha=0.9 par 10 probes, aur 0.990.99 par 100 probes tak chala jaata hai. Isliye open addressing wale tables jaldi (~0.7) resize karte hain, chaining wale thoda late (~1.0).

Resize / rehashing ka matlab: naya bada array banao (usually double size), aur saari purani keys ko nayi size ke modulo se phir se daalo. Bucket ka index h(k)modmh(k)\bmod m hota hai, aur mm badal gaya toh index bhi badal jaata hai — isliye seedha copy nahi kar sakte, har key dobara place karni padti hai. Ek rehash ka cost Θ(n)\Theta(n) hai — sun ke darr lagta hai!

Par yahaan asli jaadu hai amortized analysis. Kyunki hum size double karte hain, rehash bahut rarely hota hai. nn inserts mein total move-work 1+2+4++n2n1+2+4+\dots+n \approx 2n hi hota hai (geometric series). Toh per insert average cost constant, yaani amortized O(1)O(1). Yaad rakho: double karo, +100 mat karo. Agar fixed amount se grow karoge toh har thodi der mein rehash hoga aur total kaam O(n2)O(n^2) ho jaayega — bilkul slow. Bas yeh do cheez samajh lo: α\alpha control karta hai speed, aur doubling control karta hai resize ka cost.

Go deeper — visual, from zero

Test yourself — Hashing

Connections