Hash Hunter — Data Structures

Hunt through a hash map: 12 levels of put, get, collisions and resizing. A hash table game that shows why O(1) can turn into O(n).

What you learn in this game

A hash map stores data in "buckets" chosen by a hash function. At Mas Bayu's desk the buckets are lockers 0 to 7: a key word is turned into a number (the sum of its character codes), divided by 8, and the remainder (modulo) is its locker number.

A collision happens when two different keys give the same locker. Chaining handles it with a chain (a linked list) on every locker: a new deposit hangs at the end of that locker's chain instead of moving to another locker.

A hash set checks for duplicates in O(1): before add, check has(). The same key always lands in the same locker, so looking along that one locker's chain is enough. Comparing every pair takes O(n²); a hash set makes it O(n).

The Two Sum pattern: for each number x, check whether (target - x) is already in the map. If it is, the pair is found. If not, store x. One pass, O(n), not O(n²).

Anagram grouping: use the letter count as the key. "abc" and "cab" have the same signature, "a1b1c1", so they land in the same locker and join the same group, automatically.

The caching pattern: before computing or fetching, check the map. If it's there, return it (a hit), O(1). If not, compute it, then store it. Memoization uses the same idea for expensive results.

What the levels ask

  1. Work out hash("cat") % 8: add up the character codes of "cat", then find the remainder when dividing by 8. Tap the locker with that number.
  2. Bagas picks the key "dog". Work out hash("dog") % 8, then tap its locker.
  3. Nenek Marni knows one English word: "hi". A short word, a quick hash: work out hash("hi") % 8 and tap its locker.
  4. The locker a hash gives can already be taken: that's a collision. Mas Bayu's rack uses chaining: the deposit still goes to its hash's locker and hangs on that locker's chain. Work out hash("bat") % 8.
  5. One locker's chain already holds two deposits. Bang Arka comes with the key "ace": work out hash("ace") % 8 and hang his sandals on the right chain.
  6. The balai is filling up and there are collisions everywhere. Pak Harun leaves his sandals under the key "go": find its locker.
  7. Bu Sari wants to use the key "cat" too. One key may hold only one deposit, so first check whether "cat" is already there: tap the locker where "cat" has to be.
  8. Two Sum with target 9: the numbers 11, 15 and 7 are already stored, each number as its key. Now 2 arrives, needing the partner 9 - 2 = 7. Look up the key "7": work out hash("7") % 8 and tap its locker.
  9. Counting the letters of "hello" one by one: "h", "e" and "l" have each been counted once. The next letter is "l" again: tap the locker that keeps the count for "l".
  10. Grouping anagrams: the key is the letter signature. "abc" is already stored under the key "a1b1c1". Now "cab" arrives, and its signature is "a1b1c1" too: tap its group's locker.
  11. Kak Piksel's cache: the answer to each request is stored with the request as its key. "GET:/user" comes in again: work out its hash and tap its locker. Is it on that chain? That's a cache hit.
  12. Pak Karim has borrowed the chains, so now it's open addressing: one deposit per locker. Is the hash's locker taken? Move on to the next locker (linear probe) until you find an empty one. Pak RT arrives with the key "pen".