DrawCode Algorithm Visualizer • Hashing • Hard

Cuckoo Hashing

Tags: Hash Table, Two Choices, Eviction

1. One-Liner

Cuckoo hashing gives each key two possible slots; on collision the occupant is evicted and reinserted—like a cuckoo pushing eggs—until success or a detected cycle triggers rehash.


2. The Problem It Solves

Open addressing can probe long; chaining uses extra pointers. Cuckoo offers O(1) worst-case lookup with two hashes and constant slots per key—at the cost of tricky inserts.


3. The Core Idea

Try slot A or B; if full, kick the old key to its alternate location; repeat. Lookup always checks at most two cells.


4. How It Works (Step-by-Step)

StepWhat Happens
1Compute i1=h1(k), i2=h2(k)
2Prefer empty among the two
3Else evict, insert k, reinsert evicted
4Cycle / max steps → rehash bigger table

5. Dry Run Example

Two slots per key; insert chain evicts along path until placement or rehash—lookup still 2 probes max.


6. Key Properties

PropertyValue
LookupO(1) worst-case (two cells)
InsertAmortized expected O(1), rehash rare with low load
LoadTypically <50% for two tables

7. Where It Is Used

DomainUse
NetworkingHigh-speed exact lookups
LibrariesSpecialized maps / GPU-friendly variants

8. Interview Tips

Two hash functions, eviction path, load threshold, rehash on failure—core talking points.


9. Comparison with Other Algorithms

SchemeLookup worst
CuckooO(1) cells checked
Linear probeO(n) chain
ChainingO(length of list)

10. Complexity

MetricValue
LookupO(1) time, 2 memory refs
InsertExpected O(1), rehash amortized

Implementation Example (PYTHON)

class Cuckoo:
    def __init__(self, m=16):
        self.m, self.t = m, [None] * m
    def _h(self, k, z): return (hash(k) + z * hash(str(k))) % self.m
    def put(self, k, v, maxloop=64):
        cur, val = k, (k, v)
        for _ in range(maxloop):
            for z in (0, 1):
                i = self._h(cur, z)
                if self.t[i] is None:
                    self.t[i] = val; return
            i = self._h(cur, 0)
            self.t[i], val = val, self.t[i]
            cur = val[0]
        raise RuntimeError('rehash')

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer