781. Build a hash map with linear probing, tombstones and resizing
Interviewers like to ask how a hash table deals with collisions and growth, and then ask you to build one. Here you build an open addressing table with linear probing, the same family of design many language runtimes use for their dictionaries.
Write run_hashmap(ops, capacity=8). Store entries in a Python list of slots of length capacity; do not use a dict or set for storage. A key's home slot is hash(key) % capacity. On a collision, try the next slot (wrapping around) until you find the key or an empty slot. Never probe more than capacity slots.
Operations are tuples. ('put', key, value) inserts or updates and outputs None. ('get', key) outputs the value or None. ('remove', key) outputs True if the key was there, else False. A removed slot becomes a tombstone so later probe chains are not cut. ('size',) outputs the number of live keys. ('capacity',) outputs the current number of slots.
Growth rule: after a put that adds a new key, if size / capacity > 0.75, double the capacity and re-insert every live entry (tombstones are dropped). A put may reuse a tombstone slot, but only after confirming the key is not further along its probe chain.
def run_hashmap(ops, capacity=8)Example 1
Input: with capacity 8, keys 1 and 9 share home slot 1. run_hashmap([('put', 1, 'a'), ('put', 9, 'b'), ('remove', 1), ('get', 9)])
Output: [None, None, True, 'b']
Example 2
putting the keys 0 to 6 into capacity 8 grows the table to 16 on the seventh put, because 7 / 8 > 0.75.
Example 3
Input: ops = [('put', 1, 'a'), ('put', 9, 'b'), ('get', 9), ('get', 1), ('remove', 1), ('get', 9), ('get', 1), ('size',)]
Output: [None, None, 'b', 'a', True, 'b', None, 1]
Constraints
- Up to 100,000 operations.
- Keys are hashable (ints or strings); values are any Python objects.
Hint 1
Return both the found slot and the first reusable slot from one helper.
Hint 2
Tombstones continue the search; empty slots stop it.
Hint 3
Re-insert into the new table with the same helper, so new home slots use the new capacity.
Common mistakes
- Emptying a slot on remove, which makes keys behind it unreachable.
- Inserting into the first tombstone before checking the rest of the chain, which duplicates keys.
- Copying slots to the same index after resizing instead of re-hashing.
- Probing forever when the table is full of tombstones.
Understand the idea
A hash table maps a key to a slot with hash(key) % capacity. Two keys can land on the same slot, a collision. Chaining keeps a small list per slot; open addressing keeps one entry per slot and probes onward. Deleting from an open addressing table cannot simply empty the slot, because that would cut the probe chain for keys that collided past it, so it leaves a tombstone. As the table fills, probe chains grow, so the table doubles once a load factor is passed and every entry is re-hashed, since the slot depends on capacity.
Similar questions
- Target-encode a category with smoothingHard
- Fit imputation, scaling and one-hot encoding on train and apply them to testHard
- Estimate Jaccard similarity between documents with MinHash signaturesHard
- Split folds by user so no user lands in both train and validationHard