"Is This Thread-Safe?" How to Answer the Concurrency Follow-Up in a Coding Interview

"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:

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 get and put at 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:

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. capacity never changes after construction, so it's safe. Everything in get that touches the list is a write, even though get sounds 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 put with new keys when the cache is full. Both check size == 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 get and put each 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:

  1. Read-write lock. Many readers at once, writers get exclusive access. Watch out: in an LRU, get changes the order, so it's a write. Say that, or suggest approximate recency so reads don't need the write lock.
  2. 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.
  3. 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.
  4. 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:

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:

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.