Build a hash map, make it thread-safe, and review a producer-consumer queue
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's technical phone screen, last asked Apr 2026. It tests hash tables, concurrency and code review.
An implementation-heavy coding round. First, build a hash map from scratch and make it safe for concurrent use. Then review a producer-consumer program built on a bounded message queue and find its synchronization bugs; to make that checkable, you detect them from an execution trace.
Part 1: a hash map from scratch
Implement an integer-to-integer map without using the language's built-in map for storage.
Unless you agree on a different interface with the interviewer, assume:
-
put(key: int, value: int) -> None— inserts or overwrites. -
get(key: int) -> int— the value, or-1if absent. -
remove(key: int) -> int— removes the key and returns its value, or-1if absent.
put(1, 10); put(2, 20); put(1, 15)
get(1) -> 15
remove(2) -> 20
get(2) -> -1
remove(7) -> -1
Cover hashing and negative keys, collisions (separate chaining or open addressing with tombstones), resizing at a load factor, and average O(1) operations.
Part 2: make it thread-safe
Discuss one global lock, lock striping (a lock per group of buckets), and read-write locks; what to do about resizing while other threads read and write; and why if get(k) == -1: put(k, v) is not atomic even when each call is.
Part 3: find the bugs in a bounded queue from its trace
A producer-consumer program uses a FIFO queue with a fixed capacity. Instead of reading its source, you get the sequence of events it produced and report which kinds of bugs appear.
check_trace(capacity: int, events: list[tuple]) -> list[str]- Events:
("push", item),("pop", item)(the consumer says it dequeueditem),("wait_push",)(a producer slept believing the queue full),("wait_pop",)(a consumer slept believing it empty),("close",)(producers are done). - Report each code at most once, in this order:
OVERFLOW(push on a full queue),UNDERFLOW(pop on an empty queue),FIFO_VIOLATION(pop reported the wrong item),MISSED_WAKE_PUSH(wait_pushwhile not full),MISSED_WAKE_POP(wait_popwhile not empty),PUSH_AFTER_CLOSE,UNCONSUMED_ITEMS(trace ends afterclosewith items left). - A rejected push (full or closed) does not enqueue. A pop that reports the wrong item still removes the real front item.
check_trace(2, [("push", 1), ("push", 2), ("pop", 1), ("pop", 2), ("close",)])
-> []
check_trace(2, [("push", 1), ("push", 2), ("pop", 2), ("pop", 1), ("pop", 3), ("close",)])
-> ["UNDERFLOW", "FIFO_VIOLATION"]
check_trace(1, [("push", 7), ("wait_pop",), ("push", 8), ("close",), ("push", 9)])
-> ["OVERFLOW", "MISSED_WAKE_POP", "PUSH_AFTER_CLOSE", "UNCONSUMED_ITEMS"]
Constraints
- Up to 2 * 10^5 operations or events; keys, values and items fit in 64-bit integers.
Clarifying questions
- Which collision strategy does the interviewer expect, and must iteration order be defined?
- For the concurrent map, which operations need to be atomic together?
- In the trace, does the order of reported codes follow the list above or first occurrence?
What a strong answer covers
- A correct map with resizing and tests for collisions (keys that land in one bucket), overwrite, removal and missing keys.
- Concrete concurrency choices with their costs, including how resizing coordinates with readers.
- An exact simulation for the trace with the two special rules, O(events) time.
- Mapping each code back to the real bug that causes it: checking the condition with
ifinstead ofwhilearound a condition-variable wait, notifying the wrong condition, missing lock around size checks, no shutdown handling.
Follow-ups
- Make the map's reads lock-free.
- Add an LRU eviction policy to the map with O(1) operations, then make that thread-safe.
- Write the bounded queue itself correctly with a mutex and two condition variables, including
close.
Where it's asked
- Microsoft · Technical phone screen · Last asked Apr 202660 min · One technical round
What it tests
hash tables · concurrency · code review
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
