Hash Shelf — Data Structures

Put keys into a hash table with linear probing when slots collide: 18 levels of collisions, probing and load factor. 18 levels, right in the browser.

What you learn in this game

A hash table keeps each key in one cubby worked out from the key itself, so finding it needs no look through every cubby. In the textbook the function is hash(k) = k mod N: the remainder of the key divided by the number of cubbies. Mbak Retno's shelf has 7 cubbies, so the letter for house k goes in cubby k mod 7. This is the textbook model, not what Java or Python use. The next levels show the difference.

A search in linear probing walks from cubby k mod 7 until it finds the letter, or until an empty cubby: an empty cubby means the letter is not there. So a letter taken out must never leave a plain empty cubby. Mbak Retno puts a card (a tombstone) there. A search steps over the card and goes on; a new letter may go in the first carded cubby it meets.

The courier's app is written in Java and keeps its deliveries in java.util.HashMap. That is a real hash table, and it differs from the textbook: The keys are text. String.hashCode() works out a 32 bit number from the letters. The hash is mixed. hash = h ^ (h >>> 16): the top 16 bits are XORed into the bottom 16. A power of two shelf. The cubby is hash & (n - 1); with 16 cubbies that is the last hex digit of the hash. A collision hangs. A letter whose cubby is taken is hung at the end of that cubby's chain instead of getting another cubby.

HashMap keeps threshold = capacity times 0.75. As soon as it holds more than that, resize() makes a shelf twice as big and moves every letter. The move is not random. From a shelf of n to one of 2n, a letter in cubby j moves to j or j + n, and one bit alone decides: hash & n. Each chain is split in two, its order kept.

The JavaScript specification does not say how a Map stores its keys. What it promises: the keys come out in insertion order, and finding one is on average faster than checking them all. The specification describes it as a list of records. A set of a new key adds a record at the end; a set of an old key replaces its value in place; a delete empties its record, and the list is never reordered.

A CPython 3.12 dict writes its keys in an entries book, line by line in insertion order, and uses slots (an index table, 8 at first) that hold only line numbers. A key's first slot is hash(k) & 7. If it is taken, CPython works out perturb = perturb >> 5 and then i = (i*5 + perturb + 1) & 7, with perturb starting as the hash itself. For an integer hash(k) is k. Text is different: hash("Rara") changes every time Python starts (PYTHONHASHSEED), so that nobody can prepare thousands of keys that collide on purpose and slow a server down. That is why the Python levels here use house numbers.

What the levels ask

  1. Tap the cubby the letter in Mbak Retno's hand goes into. The cubbies are numbered 0 to 6 from the left.
  2. Tap the cubby the letter ends up in.
  3. Tap the cubby the letter ends up in.
  4. Tap the cubby the letter ends up in.
  5. For a villager collecting a letter, tap the cubby where it is found.
  6. Tap the cubby the letter ends up in.
  7. Tap the cubby the letter ends up in.
  8. The 16 cubby shelf: the top row is cubbies 0 to 7, the bottom row 8 to 15. A taken cubby is not passed by; the letter is hung under it.
  9. XOR two hex digits: write both as 4 bits, and the bits that differ become 1. XOR with 0 gives back the other digit.
  10. For a villager collecting a letter, tap the cubby whose chain holds it.
  11. When moving, tap the cubby on the new shelf. The 16 cubby shelf is the old row on top and the new row right under it.
  12. The 32 cubby shelf has four rows. A letter from cubby j drops to j or j + 16, two rows below.

And more, 18 levels in all.