Concurrency in an LLD round
medium 40 minSooner or later the interviewer asks what happens when two users do it at the same time.
Watch lesson5:07
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Words in the prompt
| You hear | Think | First sentence |
|---|---|---|
| "Several gates", "many users" | Correctness | Which rule breaks if two act at once? |
| "In the background", "when it is done" | Coordination | Who waits, and who wakes them? |
| "Only five", "at most", "pool" | Scarcity | What happens to the sixth? |
| "Process them in order" | Coordination | Order over all, or per customer? |
| "Is it thread-safe?" | All three | Let me go field by field |
| "Very fast" | Price of the guard | What is inside the lock, and is it needed? |
Nobody has mentioned threads, and your design has a shared class. What do you do?
BreaksSay nothing
- How
- Hope that it does not come up.
- Limit
- It looks as if you did not see it, and the first follow-up finds you unprepared.
- Choose when
- Never.
WorksAdd locks at once
- How
- Write the class with locks from the start.
- Limit
- Costs time, and the logic is harder to read while it is still changing.
- Choose when
- When the prompt is about concurrency itself.
StrongAsk, mark, return
- How
- Ask in scope. Mark the critical lines with a comment. Return in verification with four sentences.
- Limit
- You must keep five minutes for it.
- Choose when
- The default.