Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

Open-Addressing Map with Tombstones

Prerequisites: unordered_map Buckets, Load Factor, and Rehashing, Cache Lines, Locality, and Pointer Chasing, STL Container Choice

The rehashing article ended on the layout that removes std::unordered_map's pointer chase. Build that map for integer keys and values with the simplest probing rule. A cancel that looks up an AAPL order by order_id then reads a few adjacent slots, not scattered nodes.

  • Open addressing: every entry is stored inside one contiguous array of slots. When a key's slot is taken, the map tries other slots of the same array instead of growing a chain.
  • Home slot: key mod capacity, the remainder of key / capacity taken in the range 0 to capacity - 1.
  • Linear probing: a walk from the home slot to the next slot, then the one after. The i-th slot visited is (home + i) mod capacity, wrapping from the last slot back to slot 0.
  • Probe: one slot a walk looks at. The slots in order form the walk's probe path.

What to implement

  • process_commands(capacity, commands) returns the printed lines in order. Each command is one line.
  • The map starts with capacity slots, and capacity is a power of two.
  • Store the entries in your own slot array, without std::unordered_map, std::map, or Python's dict and set.
  • On stdin, the first line is capacity, the second is the number of commands, and each remaining line is one command.
  • There are three parts, each adding behavior to the previous one. One solution must pass all ten tests.
CommandMeaningPrints
PUT key valueStore value for key, replacing any value already storednothing
GET keyLook up keyits value, or NOT_FOUND
REMOVE keyDelete key if it is present (Part 2)nothing
PROBESReport the probes of the most recent PUT, GET, or REMOVEthe count
SLOTSShow the slot arrayone token per slot
  • PROBES prints how many slots the most recent PUT, GET, or REMOVE looked at, counting the slot where its walk stopped. Before the first such command it prints 0. PROBES and SLOTS never change the count.
  • SLOTS prints one token per slot, from slot 0 to the last slot, separated by single spaces. The tokens are in the table below.

Slots and home slots

Each slot is in one of three states, and every slot starts EMPTY.

StateMeaningSLOTS token
EMPTYhas held nothing since its array was allocated.
LIVEholds one key and its valuethe key
TOMBSTONEheld a key that was removed (Part 2)T
  • Because capacity is a power of two, key mod capacity equals key & (capacity - 1), which keeps the key's low bits. This holds in both C++ and Python, and for negative keys too.
  • With 8 slots, key -1 has home slot 7.

Part 1: Insert and look up

  1. GET key walks from the key's home slot until it reaches a LIVE slot holding key or an EMPTY slot.
  2. A LIVE slot holding a different key does not stop the walk, because a colliding key may have been stored further along.
  3. If the walk finds the key, GET prints its value. If it reaches an EMPTY slot first, the key is absent and GET prints NOT_FOUND.
  4. PUT key value makes the same walk. If it finds the key, it replaces the value in that slot. If it reaches an EMPTY slot, it stores the key and value there.
  5. So a new key lands in the first EMPTY slot of its probe path, and a later GET retraces that path to find it.

Limits

  • Keys and values are signed 32-bit integers.
  • capacity is a power of two between 1 and 1024. There are at most 2 × 105 commands.
  • SLOTS appears only while the array has at most 64 slots.

Tests 1–3 cover this part. They include keys that share a home slot, a walk that wraps from the last slot to slot 0, replaced values, negative keys, and the 32-bit extremes.

These tests never fill more than three quarters of the slots, so Part 3's growth never happens in them and an EMPTY slot always remains to end a walk.

Part 2: Delete with tombstones
  1. REMOVE key walks like GET. If the walk finds the key, its slot becomes a tombstone: the slot no longer holds an entry, but unlike an EMPTY slot it does not end a walk.
  2. If the key is absent, REMOVE changes nothing. Either way, its walk sets the PROBES count.
  3. GET and REMOVE walk past tombstones and stop only at the key or at an EMPTY slot.
  4. PUT also walks until it finds the key or reaches an EMPTY slot, since the key may be stored past a tombstone. It remembers the first tombstone it passes.
  5. If PUT finds the key, it replaces the value in the key's slot. The key does not move to an earlier tombstone.
  6. If PUT reaches an EMPTY slot, the key is absent. The new entry goes into the first tombstone on the walk, or into the EMPTY slot when the walk passed no tombstone.
  7. PROBES counts the whole walk, up to the key or the EMPTY slot, even when the entry lands in an earlier tombstone.

Marking a removed slot EMPTY instead would break lookups. A key that collided with the removed one and was stored further along would become unreachable, because its GET would stop at the new EMPTY slot.

Tests 4–6 cover this part. Like tests 1–3, they never let LIVE slots and tombstones together fill more than three quarters of the array.

Part 3: Grow the array

Only an EMPTY slot ends a walk, so tombstones lengthen walks just as LIVE slots do. The map therefore counts its used slots: the LIVE slots plus the tombstones.

  1. Right after a PUT stores a new key, if 4 * used > 3 * capacity, meaning more than three quarters of the slots are used, the map grows.
  2. Growing allocates 2 * capacity EMPTY slots and reinserts every LIVE entry with its value. Tombstones are not copied, so the new array has none.
  3. The reinsertion scans the old array from slot 0 to its last slot. It puts each key in the first EMPTY slot of its probe path in the new array, whose home slots use the new capacity.
  4. A PUT that replaces a value or reuses a tombstone leaves used unchanged, so it never grows the map. REMOVE never shrinks it.
  5. After a PUT that grew the map, PROBES reports that PUT's walk in the old array; the reinsertion is not counted.

One doubling is always enough. Before the PUT, at most three quarters of the old slots were used, so the live keys fill at most half of the new array.

Growth never changes what GET prints, but it changes the layout, which SLOTS and PROBES can see.

Tests 7–10 cover this part.

Example
Input
8
9
PUT 3 30
PUT 11 110
PUT 19 190
PUT 4 40
PROBES
GET 27
PROBES
GET 11
SLOTS
Output
3
NOT_FOUND
5
110
. . . 3 11 19 4 .
Explanation

Keys 3, 11, and 19 all have home slot 3, because each leaves remainder 3 when divided by 8. Key 11 finds slot 3 taken and lands in slot 4, and 19 lands in slot 5.

Key 4's home slot is 4, which 11 already holds, so its walk looks at slots 4, 5, and 6 and stores it in slot 6. PROBES prints 3.

GET 27 also starts at slot 3 and passes four LIVE slots before it reaches EMPTY slot 7, so 27 is absent after 5 probes.