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

Chained Hash Map

Prerequisites: unordered_map Buckets, Load Factor, and Rehashing, STL Container Choice

The rehashing article keyed the live AAPL orders by order_id in a std::unordered_map and opened up the buckets, chains, and rehash underneath it. Build that structure by hand for integer keys and values.

Hashing a key picks one bucket, and different keys can pick the same bucket. Those collisions must not overwrite each other.

What to implement

  • process_commands(bucket_count, commands) returns the printed lines in order. Each command is one line.
  • The map starts with bucket_count buckets, called B below.
  • Store the entries in your own bucket array, without std::unordered_map, std::map, or Python's dict and set.
  • On stdin, the first line is bucket_count, 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 twelve 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
SIZECount the distinct keys stored (Part 2)the count
BUCKETSReport the current bucket count (Part 3)the count
CHAIN bList bucket b's keys in chain order (Part 3)the keys separated by spaces, or EMPTY

Bucket index

With n buckets, key k belongs to bucket k mod n: the remainder of k / n, taken in the range 0 to n - 1.

  • Python's k % n already gives that value.
  • In C++, k % n is negative when k is negative, so add n to a negative remainder.
  • With 5 buckets, key -1 therefore lands in bucket 4, the same bucket as key 4.

Part 1: Store and look up

  1. PUT key value finds the key's bucket and searches only that bucket's chain. If the key is there, its value is replaced; otherwise a new entry joins the chain.
  2. GET key searches the same chain and prints the stored value, or NOT_FOUND when the key is absent.
  3. The bucket count stays B in this part.

Limits

  • Keys and values are signed 32-bit integers.
  • B is between 1 and 104. There are at most 2 × 105 commands.
  • Every CHAIN b names an existing bucket: b is at least 0 and less than the current bucket count.

Tests 1–6 cover this part. They include colliding keys, replaced values, negative keys, a one-bucket map in which every key collides, and the 32-bit extremes.

Part 2: Delete and count entries
  1. REMOVE key deletes a present key and otherwise does nothing.
  2. SIZE prints the number of distinct keys currently stored, in O(1) time. Keep a counter that changes only when a PUT adds a new key or a REMOVE deletes one.
  3. Replacing the value of an existing key does not change the size.

Tests 7–9 cover this part.

Part 3: Rehash as it grows

The map now grows the way std::unordered_map does. With n the current bucket count, the load factor is size / n. The maximum load factor is 1, the std::unordered_map default.

  1. Right after a PUT adds a new key, if size > n, the map rehashes. It allocates 2n empty buckets and reinserts every entry, because each key's bucket depends on the bucket count.
  2. Reinsertion visits the old buckets in order from 0 to n - 1, walks each chain front to back, and appends each entry to the back of its chain in the new array.
  3. A PUT that only replaces a value never rehashes.
  4. The bucket count never shrinks, not even after REMOVE.
  5. A new key joins the back of its chain. REMOVE takes a key out and leaves the rest of the chain in order. Replacing a value leaves the key where it is.

Output: two commands expose the layout. BUCKETS prints the current bucket count. CHAIN b prints one line:

k1 k2 ... kc
  • k1 to kc: the keys in bucket b from front to back, separated by single spaces.
  • When the bucket holds nothing, CHAIN b prints EMPTY.

Growth never changes what GET or SIZE print, so tests 1–9 keep their outputs whether or not the map grows; only BUCKETS and CHAIN can see it.

Tests 10–12 cover this part.

Example
Input
2
8
PUT 8 80
PUT 0 5
GET 0
PUT 3 30
BUCKETS
CHAIN 0
CHAIN 1
CHAIN 3
Output
5
4
8 0
EMPTY
3
Explanation

With 2 buckets, keys 8 and 0 both land in bucket 0 and chain there, 8 first. GET 0 walks past 8 to find 0 and prints 5.

PUT 3 adds a third key to 2 buckets, so size > n and the map doubles to 4 buckets. The rehash walks old bucket 0 (8, then 0) and old bucket 1 (3).

Keys 8 and 0 leave remainder 0 when divided by 4, so bucket 0 keeps them in the same order. Key 3 leaves remainder 3, so it moves to bucket 3, and bucket 1 is left empty.