Why Concurrency Shows Up in LLD Rounds
You'll learn to
- -Recognize the shape of a problem that secretly needs thread-safety: many actors, one limited shared resource
- -Reproduce the check-then-act race on a parking lot and see the impossible result with your own eyes
- -Know why saying the problem out loud is worth marks even before you fix it
Most LLD prompts are secretly two questions. Can you design clean, extensible classes? And can you notice that this class diagram will break when several things use it at once? A design that is beautifully structured but lets two threads give the same parking spot to two cars has failed the second, unstated question.
An Analogy: The Last Cookie
Two children both see one cookie left in the jar. Each thinks "there is one, I'll take it", and each reaches in at the same moment. Both believe they got the last cookie, and the jar is somehow "minus one". The problem is not that either child is careless. It is that checking the jar and taking the cookie are two separate steps, and the other child can act in between.
The Shape That Signals "This Needs Thread-Safety"
The tell is always the same: several independent actors (users, requests, threads) competing for a limited, shared, changeable resource. A thread, again, is one independent line of execution, so several threads running together are the "actors". A parking lot has a fixed number of spots that many cars try to claim. A movie-booking system has a fixed number of seats that many users try to reserve. A rate limiter has a shared counter that many requests increment. Whenever your design says "there are N of these, and more than N requesters might show up at once", concurrency is the core of the problem, not an edge case.
- -A counter or collection read and written by several actors, like a rate limiter's request count.
- -A finite pool being claimed: parking spots, theatre seats, database connections.
- -A "check, then act" sequence, such as checking a spot is free and then assigning it. This is the classic race condition, a bug whose result depends on the unlucky timing of two threads.
The Check-Then-Act Race, Made Concrete
The demo reproduces the bug on purpose. A parking lot has exactly one spot left, and two cars arrive together. The short sleep stands in for the unlucky moment when the other thread gets to run between the check and the act. In real code that window is tiny, but under load it is hit constantly.
Both threads read 1, both passed the check, and both decremented. Two cars were promised the single remaining spot, and the lot reports minus one spot left. Nothing crashed and no error appeared, which is what makes this kind of bug dangerous. The fix (a lock, an atomic operation, or restructuring so the check and the act happen as one step) is the subject of the next chapters.
Noticing a concurrency problem out loud, even without time to fully implement the fix, is worth more in an interview than silently producing a design that compiles but hides a race.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!