3.3.1Hashing

Hash function — properties - deterministic, uniform, fast

2,047 words9 min readdifficulty · medium

WHAT is a hash function?

Because Um|U| \gg m, collisions are unavoidable (pigeonhole). Two different keys will sometimes map to the same bucket. A good hash function doesn't avoid collisions entirely — it makes them rare and evenly distributed.


The three core properties

1. Deterministic

WHY this matters: Hashing works by put then find. If you insert("cat") it lands in bucket 7. Later lookup("cat") must compute bucket 7 again to find it. If hh used randomness or the current time, "cat" would land somewhere else and you'd never retrieve it.


2. Uniform (good distribution)

WHY this matters — derive the cost. Let nn keys be inserted into mm buckets. Define the load factor: α=nm=number of keysnumber of buckets\alpha = \frac{n}{m} = \frac{\text{number of keys}}{\text{number of buckets}}

Derivation of expected chain length (separate chaining): For a fixed bucket jj, let indicator Xi=1X_i = 1 if key ii lands in bucket jj. E[Xi]=P(h(ki)=j)=1mE[X_i] = P(h(k_i)=j) = \frac{1}{m} Expected keys in bucket jj = sum over all nn keys (linearity of expectation): E ⁣[i=1nXi]=i=1n1m=nm=αE\!\left[\sum_{i=1}^{n} X_i\right] = \sum_{i=1}^{n} \frac{1}{m} = \frac{n}{m} = \alpha

So search cost ≈ O(1+α)O(1 + \alpha) (1 to compute the hash, α\alpha to walk the chain). If α\alpha stays a small constant (we keep mnm \approx n), search is O(1)O(1).

Figure — Hash function — properties -  deterministic, uniform, fast

3. Fast (efficient to compute)

WHY this matters: The whole selling point of a hash table is O(1)O(1) operations. If hh itself took O(n)O(n) or did something expensive (cryptographic hashing, sorting), you'd lose the advantage. Hashing trades a tiny constant-time computation for avoiding a search.


Worked examples


Recall Feynman: explain to a 12-year-old

Imagine a wall of mailboxes numbered 0 to 9. A hash function is a rule that decides which mailbox each letter goes into based on the name on it. Good rule: (1) Same name always same box — otherwise you'd never find your letter again (deterministic). (2) Spread letters evenly — you don't want all letters jammed in box 3 and the rest empty (uniform). (3) The rule is quick to apply — you shouldn't spend an hour deciding the box (fast). Sometimes two names land in the same box (a collision) — that's okay, you just keep a little stack inside that box.


Flashcards

What three properties define a good hash function?
Deterministic (same key → same bucket), Uniform (spreads keys evenly), Fast (cheap to compute, O(1)O(1) or O(L)O(L)).
Why must a hash function be deterministic?
So that lookup recomputes the SAME bucket where insert placed the key; otherwise retrieval is impossible.
Define the load factor α\alpha.
α=n/m\alpha = n/m = number of keys ÷ number of buckets; expected chain length under uniform hashing.
What is the expected search cost with separate chaining?
O(1+α)O(1 + \alpha) — derived via linearity of expectation, each bucket holds α\alpha keys on average.
Why prefer a prime table size mm in the division method?
A prime mod mixes all bits/digits of the key; powers of 2 or 10 only use low-order bits → clustering.
Why are collisions unavoidable?
Pigeonhole: Um|U| \gg m, so more possible keys than buckets — some must share.
Why use weights pip^i in a string hash instead of summing ASCII?
Summing makes anagrams collide ("abc"="cab"); positional weights distinguish character order.
What does Simple Uniform Hashing assume?
Each key is equally likely (1/m1/m) to land in any bucket, independent of other keys.
When should you resize/rehash a hash table?
When α\alpha exceeds a threshold (~0.75) so chains stay short and operations stay O(1)O(1).
Why can't a hash function use a fresh random number per call?
It would violate determinism — same key would hash differently each time, breaking lookups.

Connections

  • Hash Table — the data structure these functions power.
  • Collision Resolution — Separate Chaining — what handles the unavoidable collisions.
  • Collision Resolution — Open Addressing — alternative needing especially uniform hashes.
  • Load Factor and Rehashing — keeps α\alpha small to preserve O(1)O(1).
  • Modular Arithmetic — basis of the division method.
  • Pigeonhole Principle — why collisions must exist.
  • Cryptographic Hash Functions — contrast: strong & slow vs. our cheap & fast.

Concept Map

mapped by

indexes into

much larger than m so

must be

must be

must be

enables

by linearity gives

equals

determines

keeps cost

prevents long chains from

Hash function h maps U to m buckets

Universe U of keys huge

Table of m buckets small

Collisions unavoidable

Deterministic

Uniform SUH

Fast to compute

Load factor alpha = n/m

Expected chain length

Search O of 1 + alpha

Reliable put then find

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho ek hash function ek chhota sa "rule" hai jo kisi bhi key (string, number) ko ek bucket number me convert kar deta hai. Hashing ka pura magic isi pe tika hai ki yeh rule teen cheezein follow kare. Pehli: Deterministic — same key daalo to hamesha same bucket aaye. Agar aisa nahi hua to jo cheez insert ki thi woh lookup ke time milegi hi nahi, kyunki bucket badal jaayega. Isliye random() ko function ke andar mat use karo.

Doosri: Uniform — keys saare buckets me barabar phailni chahiye. Agar saari keys ek hi bucket me jam gayi (clustering), to wahan ek lambi chain ban jaayegi aur search O(1)O(1) se O(n)O(n) ki taraf girne lagega. Yahan load factor α=n/m\alpha = n/m aata hai — average me ek bucket me itni keys hoti hain. Linearity of expectation se prove hota hai ki expected chain length exactly α\alpha hoti hai, isliye search cost O(1+α)O(1+\alpha). Agar mm ko nn ke aas-paas rakho to α\alpha chhota constant rahega aur sab kuch O(1)O(1).

Teesri: Fast — hash compute karna sasta hona chahiye, warna hash table ka speed advantage hi khatam ho jaata. Isliye simple operations use karte hain: multiply, add, mod. String ke liye polynomial hash use hota hai — har character ko pip^i weight do (Horner method se), taaki "abc" aur "cab" alag-alag hash de. Aur ek tip: table size mm ko prime rakho, kyunki power-of-2 ya power-of-10 sirf last few bits/digits dekhte hain aur clustering ho jaati hai. Yaad rakhne ke liye DUF — Deterministic, Uniform, Fast.

Go deeper — visual, from zero

Test yourself — Hashing

Connections