← chapter

Hash tables

O(1) lookup by any key: hash it to a bucket. The structure behind every dictionary.

The idea, and the catch

Watch collisions form chains

3, 11, 19 all hash to bucket 3 → a chain of three. Lookup scans just that short chain.

The load factor is the dial

α=n/m\alpha = n/m — items per bucket

Scratch vs library

Open addressing beats chaining (cache). dict beats both (C). All O(1).

Takeaway

Average O(1) get/put/delete — as long as the load factor stays bounded. No order, though. Next: the hash function itself.