III · Hashing & lookup

Cuckoo hashing

Two slots, no chains.

The story

Two Danish researchers solved the worst-case-O(1) lookup problem. Each key has two possible homes; on collision, the new key kicks the old one out (the "cuckoo") to its other home. Looks impossible until you read the analysis — when load factor is below 50%, infinite chains are vanishingly unlikely.

How it works

Two hash tables, two hash functions. Lookup checks both. Insert: place at h1; if occupied, evict that key to its h2; if that's occupied, evict it to its h1; continue until empty slot or rehash.

Where it lives

Cuckoo filter (a Bloom filter alternative). Some routers' MAC tables. Memcached extensions. CockroachDB transaction status records.

The key insight

Cuckoo hashing's worst-case-O(1) lookup is rare and valuable — most hash tables only guarantee O(1) on average. Used where adversarial inputs make average-case bounds insufficient.