Animation9 min

Watch a Hash Map Work

Frequency counting and complement lookup animated.

This animation makes the hash map's O(1) promise physical. Array items ride a conveyor belt across the top; below sits the map, drawn as labeled buckets. As each item arrives, its key flies into the right bucket and that bucket's count badge bounces up by one — frequency counting, one constant-time step per item.

Then the scene switches to Two Sum mode. For each arriving number x, a ghost outline of the complement it needs (target − x) appears and is searched among the buckets. Misses fade out and x drops into its own bucket; on a hit, a green line connects the two partners and the run ends.

What to watch for:

  • Each item does constant work. One bucket probe, maybe one insert — no rescanning of earlier items, ever. That's the whole reason one pass suffices.
  • The check happens before the insert. Watch the order: the ghost complement is searched first, then x is filed away. Reversed, a number like 4 with target 8 would match itself.
  • The buckets are memory you paid for. By the end, the map holds up to n entries — the O(n) space you traded for O(n) time.