Design HashMap
Hash Table Implementation
Build a key-value store with put, get and remove in O(1) average time: a bucket array, a hash function, chaining for collisions, and a resize when the table fills up.
Overview
A hash map stores values by key and finds them again without scanning. It turns the key into a number (the hash), turns that number into an array index (the bucket), and looks only in that bucket. Two keys that land in the same bucket are a collision, and handling collisions is most of the design.
The interview version asks for put(key, value), get(key) and remove(key), each in O(1) time on average. You are expected to write the bucket array and the collision handling yourself, not to wrap the language's built-in map. The problem is short, so interviewers use it to watch details: how you map a hash to an index, how you update an existing key without adding a duplicate, when you resize, and why every entry has to be rehashed when you do.
A strong answer names the three parts in the first minute (hash function, bucket array, chains), states the load factor rule before writing code, and mentions what the real libraries do: Java's HashMap starts with 16 buckets and resizes at a load factor of 0.75; CPython's dict uses open addressing and resizes when it is two thirds full.
Scoping the problem
Ask a few questions before you write code. Each one changes the design a little, and each has a usual answer.
- What are the keys? Any hashable object. The map only needs keys that can be hashed and compared for equality.
- What does get return for a missing key? It could be None, a default, or -1 as in the LeetCode version. The code below takes a default.
- Should remove report whether the key existed? It decides the return type. Here it returns True or False.
- Fixed size or growing? A fixed table degrades to long chains, and a growing one needs resize logic. The map grows by doubling.
- Chaining or open addressing? They are two different collision designs with different code. Use chaining, unless asked otherwise.
- Shared by many threads? It adds a lock and decides how fine-grained it is. Treat it as a follow-up, not the core.
Functional requirements
- put(key, value) inserts a new entry or replaces the value of an existing key without creating a duplicate
- get(key, default) returns the value, or the default when the key is absent
- remove(key) deletes the entry and returns True, or returns False when the key is absent
- The map grows: when size / capacity passes the load factor, capacity doubles and every entry is rehashed
- Collisions are handled by chaining: each bucket is a linked list of entries
- Any hashable key is accepted; an unhashable key raises TypeError
Non-functional requirements
- get, put and remove run in O(1) time on average, with resize O(1) amortized
- Worst case O(n) when every key lands in one chain
- Memory is O(n + capacity): one entry node per key plus the bucket array
- Initial capacity and load factor are constructor arguments with sensible defaults (16 and 0.75)
Interview questions
- Why did you choose chaining over open addressing?
- Why does resize have to rehash every entry, and why is it still O(1) amortized?
- How would you make this map safe for many threads?
- What makes a key a good key, and what goes wrong with a bad one?
- What bugs do people write in this problem, and how do you catch them?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio