"Is This Thread-Safe?" How to Answer the Concurrency Follow-Up in a Coding Interview
You've just finished an LRU cache, a rate limiter, or a simple counter class. The tests pass in your head and the interviewer nods. Then they ask: "What happens if two threads call this at the same time?"
A lot of candidates freeze here. Concurrency feels like a different subject, and it can sound like the interviewer is asking you to rebuild everything with locks. Usually they aren't. They want to know whether you can spot shared state, say what breaks, and suggest a reasonable fix while being honest about the cost. You can learn to do that in a few minutes, and you can practice it.
This guide gives you a repeatable way to answer, with examples you can adapt.
Why interviewers ask the thread-safety follow-up
This question tends to come after a working solution, especially for mid-level and senior roles. It checks:
- Production thinking. Real services handle many requests at once. A cache or counter that's only correct on one thread is a bug waiting to happen.
- Reasoning about invariants. Can you say what has to stay true (for example, "the map and the linked list always hold the same keys") and where that could break?
- Trade-off judgment. Do you go straight for a global lock, or can you compare a coarse lock, finer-grained locks, and atomic or concurrent data structures?
- Calm under a curveball. As with the space or scaling follow-ups, how you handle a surprise counts as much as the final answer.
The interviewer rarely expects a perfect lock-free design. A clear, correct, simple answer usually beats a clever one you can't explain.
Step 1: Restate the scenario in one sentence
Don't jump straight to "add a mutex." First confirm what's being asked:
"So we have multiple threads sharing one instance of this cache, calling
getandputat the same time. Is that the setup you have in mind?"
This buys you a few seconds and shows you're precise. Sometimes the interviewer adds a constraint, like "reads are 95% of traffic," and that changes the best answer.
Step 2: Point at the shared mutable state
Go through your code out loud and name everything that's shared and mutable:
- Instance fields such as maps, lists, counters, and size variables
- Static or global variables
- Any object that more than one call can reach and change
Then name what's safe: local variables, immutable inputs, and values that are only read.
"The shared state is the hash map and the doubly linked list.
capacitynever changes after construction, so it's safe. Everything ingetthat touches the list is a write, even thoughgetsounds like a read, because it moves the node to the front."
That last point is the kind of detail that stands out. A "read" that changes ordering or counters is a write.
Step 3: Show one concrete race
Abstract answers like "it could have race conditions" sound vague. Walk through one specific interleaving:
"Say two threads call
putwith new keys when the cache is full. Both checksize == capacity, both decide to evict the tail, and both remove a node. We could evict two entries when we meant to evict one, or worse, both unlink the same node and corrupt the list pointers."
For a counter:
"
count++is really read, add, write. Two threads can both read 5, both write 6, and we lose an increment."
One clear race is enough. It proves you understand the problem and sets up the fix.
Step 4: Offer the simple fix first
Start with the simplest correct option:
"The simplest fix is one lock around every public method, so
getandputeach take the same mutex. That makes each operation atomic relative to the others, and the invariant between the map and the list always holds."
Then say what it costs:
"The downside is contention. Every call is serialized, so under heavy load threads wait on each other even when they touch different keys."
Saying the cost out loud is usually what separates a strong answer from an average one.
Step 5: Improve only if it's worth it
If the interviewer keeps going ("what if this is a hot path?"), climb the ladder one step at a time:
- Read-write lock. Many readers at once, writers get exclusive access. Watch out: in an LRU,
getchanges the order, so it's a write. Say that, or suggest approximate recency so reads don't need the write lock. - Lock striping or sharding. Split keys across N segments, each with its own lock and its own small LRU. Throughput goes up, but eviction becomes per-shard, which only approximates global LRU.
- Concurrent library structures. Use your language's concurrent map plus atomic counters for simple cases. Mention that combining two concurrent structures doesn't make the combination atomic.
- Atomics for single values. For a plain counter, an atomic increment or compare-and-swap is enough. No lock needed.
You don't need to implement all of these. Name one or two, explain when you'd pick each, and ask which direction the interviewer wants to go.
Step 6: Mention the traps
A sentence or two on common pitfalls shows depth:
- Check-then-act. "If key not in map, insert" is two steps. Without a lock or an atomic
putIfAbsent, another thread can slip in between them. - Deadlock. If you ever hold two locks, always take them in the same order.
- Holding a lock during slow work. Don't call the network or disk while holding a lock. Compute outside, then lock briefly to update.
- Visibility. Without proper synchronization, one thread may not see another's write. Locks and atomics handle this for you, and plain fields don't.
A sample answer, start to finish
Here's roughly how a strong 90-second answer to "Is your rate limiter thread-safe?" might sound:
"Not as written. The shared state is the per-user timestamp deque in the map. Two requests for the same user could both see 9 entries in the window, both decide they're under the limit of 10, and both append, so we'd allow 11. The simplest fix is a lock per user ID, so different users don't block each other but the check-and-append for one user is atomic. If memory for per-user locks is a concern, I'd stripe them, for example 64 locks chosen by hashing the user ID. If an approximate limit is acceptable, a fixed-window counter with an atomic increment avoids locking entirely. Which constraint matters most here?"
It's short, specific, and ends by handing the conversation back to the interviewer.
How to practice this follow-up
Concurrency questions feel scary mostly because people rarely practice saying the answer out loud under time pressure. A few habits help:
- After every design-style coding problem (cache, counter, queue, rate limiter, bank account), add one extra minute: "What breaks with two threads?"
- Use the same script every time: restate the scenario, name the shared state, show one race, give the simple fix and its cost, then describe one improvement.
- Time yourself. Aim for a clear answer in under two minutes.
- Record or narrate your answer so you can hear where you hedge or ramble.
If you want structure for these drills, Luna Interview is a Chrome side-panel interview prep tool that keeps your notes, follow-up checklists, and timed practice next to your editor, so you can rehearse follow-ups like this one in realistic conditions. You can find it at https://lunainterview.xyz/.
FAQ
Do I need to write the locking code? Usually not, unless they ask. Describing where the lock goes and what it protects is often enough. If they want code, wrap the body of each public method in a lock and keep it short.
What if I don't know my language's concurrency APIs well? Explain the idea in general terms ("a mutex around this block," "an atomic increment"). Interviewers care more about the reasoning than the exact API name, and you can say you'd check the docs.
Is a single global lock a bad answer? No. It's a correct starting point. It only looks weak if you can't explain its cost or suggest anything better when asked.
Can this come up in junior interviews? Less often, but it does happen. Even a short "the map is shared, so I'd guard it with a lock" is a solid answer at that level.
Rehearse with an AI interview copilot, then browse commonly reported interview questions.