Design a Concurrent Hash Map
🗂️ What is a concurrent hash map? It is a hash map that many threads can use at the same time without corrupting it or losing writes. A good one does more than stay correct: threads that touch different keys should almost never wait for each other, so adding threads adds throughput.
This question sits between data structures and concurrency. Everyone can write a hash map, and everyone can put one lock around it. The interview is about what comes after that: how to let threads work in parallel (lock striping), how to make "check, then update" safe, and how to grow the table while other threads are using it, without a deadlock. The comparison with one global lock runs through the whole post, because knowing when the simple lock is good enough is part of a strong answer.
Requirements
"Design a hash map that many threads can read and write at the same time."
The prompt hides most of the decisions. A few questions turn it into rules.
Clarifying Questions
You: Can I wrap my language's dictionary in a lock, or should I build the table myself?
Interviewer: Build the table yourself, with buckets. I want to see where the locks go, and that depends on the table's layout.
You: What is the workload? Many threads on the same few keys, or spread over many keys?
Interviewer: Spread out, mostly. Dozens of threads, thousands of keys, reads and writes both common. Threads working on different keys should not slow each other down.
You: Besides get, put and remove, do callers need operations that read and then write, like "add if missing" or "add one to a counter"?
Interviewer: Yes. putIfAbsent and increment are both used a lot. Callers count events per key from many threads, and no count may be lost.
You: Does the table grow?
Interviewer: Yes. Start with 16 buckets and double when it is more than three-quarters full. It never shrinks.
You: How exact does size() need to be, and how often is it called?
Interviewer: Dashboards call it now and then. It should be cheap, and exact whenever no writes are in flight.
You: Do we need iteration over all entries?
Interviewer: Not today.
You: Can values be missing or null?
Interviewer: Values are non-negative integers. Return -1 for a missing key.
The third answer is the one candidates most often skip. A thread-safe get and a thread-safe put do not add up to a thread-safe "get, add one, put". We will see that bug in code, and it decides what our public methods look like.
💡 Interview tip: When a prompt says "thread safe", ask which compound operations callers need. Making each method safe on its own is the easy half. The hard half is the operations that callers would otherwise build out of two calls.
Final Requirements
put,getandremovebehave like a normal map:putreturns the old value or-1,getandremovereturn-1for a missing key.- Any number of threads may call any method at the same time. No update is ever lost and the table is never corrupted.
- Threads working on keys in different parts of the table do not block each other.
put_if_absent(key, value)andincrement(key, delta)are atomic: no other thread can change the key between their check and their update.- The table starts at 16 buckets. Right after a new key is added, if
size > 0.75 × buckets, the bucket count doubles. It never shrinks. size()is cheap and exact when no writes are in progress.
Out of scope
- Iteration and snapshots (we come back to them in Extensibility).
- Shrinking, persistence, and expiry.
- Lock-free algorithms built on compare-and-swap.