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_countbuckets, calledBbelow. - Store the entries in your own bucket array, without
std::unordered_map,std::map, or Python'sdictandset. - 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.
| Command | Meaning | Prints |
|---|---|---|
PUT key value | Store value for key, replacing any value already stored | nothing |
GET key | Look up key | its value, or NOT_FOUND |
REMOVE key | Delete key if it is present (Part 2) | nothing |
SIZE | Count the distinct keys stored (Part 2) | the count |
BUCKETS | Report the current bucket count (Part 3) | the count |
CHAIN b | List 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 % nalready gives that value. - In C++,
k % nis negative whenkis negative, so addnto a negative remainder. - With 5 buckets, key
-1therefore lands in bucket 4, the same bucket as key4.
Part 1: Store and look up
PUT key valuefinds 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.GET keysearches the same chain and prints the stored value, orNOT_FOUNDwhen the key is absent.- The bucket count stays
Bin this part.
Limits
- Keys and values are signed 32-bit integers.
Bis between 1 and 104. There are at most 2 × 105 commands.- Every
CHAIN bnames an existing bucket:bis 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
REMOVE keydeletes a present key and otherwise does nothing.SIZEprints the number of distinct keys currently stored, in O(1) time. Keep a counter that changes only when aPUTadds a new key or aREMOVEdeletes one.- 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.
- Right after a
PUTadds a new key, ifsize > n, the map rehashes. It allocates2nempty buckets and reinserts every entry, because each key's bucket depends on the bucket count. - 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. - A
PUTthat only replaces a value never rehashes. - The bucket count never shrinks, not even after
REMOVE. - A new key joins the back of its chain.
REMOVEtakes 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
k1tokc: the keys in bucketbfrom front to back, separated by single spaces.- When the bucket holds nothing,
CHAIN bprintsEMPTY.
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.
2 8 PUT 8 80 PUT 0 5 GET 0 PUT 3 30 BUCKETS CHAIN 0 CHAIN 1 CHAIN 3
5 4 8 0 EMPTY 3
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.