"what happens if two cars arrive at the same second"
i finished the parking lot. all of it. floors, slots, a strategy for slot allocation, a pricing strategy, ticket issue and exit, running code with a demo in main. fifteen minutes left on the clock and i was feeling good.
then: "you have ten entry gates. two cars pull in at the same instant and there's one compact slot left. walk me through what happens."
and i said "i'd add synchronized."
"where?"
that's where it fell apart. i genuinely did not know where. i said the park method, which is half right by accident, and then he asked what happens to the other nine gates while that lock is held, and i had nothing. the rejection note said "limited depth on concurrent access."
i've since had a version of that exact question in three separate machine coding rounds. rate limiter, wallet, seat booking. same question wearing a different hat every time. so this is the writeup i wish someone had handed me.
why leetcode structurally cannot prepare you for this
every problem on leetcode runs your solution on exactly one thread. not "usually." by construction. the harness calls your function, your function returns, nothing else in the universe touches your state while you're in there.
that means you can have 600 problems solved and have literally never written a line of code that two threads execute at once. it isn't a gap in your effort. the format has no way to test it.
leetcode does have a concurrency tag. it's under a dozen problems and it's the emptiest room on the site — 1114 print in order, 1115 print foobar alternately, 1116 print zero even odd, 1117 building h2o, 1188 design bounded blocking queue, 1195 fizz buzz multithreaded, 1226 the dining philosophers. i had solved zero of them at the time of that interview. i'd guess most people reading this have too.
meanwhile the machine coding round asks about it basically every time, because it's the cheapest way for an interviewer to find out whether you've shipped anything or only studied.
the whole thing is one bug
here's what took me way too long to see. every concurrency follow-up in an LLD round is the same bug. not a family of bugs. one bug.
if (something is still true) {
act on it
}you check. then you act. and between the check and the act, another thread checks too. both see the same true, both act, and the invariant you were protecting is gone.
it's called check-then-act, and once you have that name, the follow-up stops being a surprise question and becomes a search: where in my design is there a check-then-act on shared mutable state?
the five places it's hiding in problems you've already designed
parking lot — slot = findAvailableSlot() then slot.occupy(car). two gates find the same slot object, both occupy it. one car is now parked in a slot the system believes belongs to someone else, and your exit flow charges the wrong ticket.
wallet / payment — read balance 100, check that 100 >= 80, deduct 80. two debits of 80 land together, both read 100, both pass the check, and you've written a negative balance. this is the canonical version and it's worth being able to draw on a whiteboard in ten seconds.
rate limiter — if (count < max) count++. people say "i made count an AtomicInteger so it's safe." two atomic operations are not one atomic operation. the read and the increment are separately atomic and the gap between them is exactly the hole. with 50 threads on a limit of 100 you'll let noticeably more than 100 through. you want a single compare-and-set loop, or the count and the window under one lock.
seat / slot booking — isAvailable(slot) then book(slot). double booking. this one is the worst to admit in an interview because the consequence isn't a corrupted field, it's two customers holding a confirmation for the same seat and a refund conversation.
inventory — check stock >= qty, then decrement. oversell. same shape.
five different problems. one bug. and the thing that actually scores is saying the name out loud: "this is check-then-act — the check and the act have to be in the same critical section, otherwise the check tells me nothing by the time i use it."
three things that sound thread-safe and are not
this is the part that separated my failing answers from my passing ones, because a wrong confident answer here reads worse than "i'm not sure."
1. "i used a ConcurrentHashMap, so it's thread-safe."
no. each individual operation on it is atomic. your sequence of operations isn't.
if (!map.containsKey(userId)) {
map.put(userId, new Wallet());
}two threads, two wallets, one of them silently thrown away along with whatever was in it. the map did its job perfectly; your compound operation was never atomic. what you want is the API that makes the whole check-then-act one call — putIfAbsent, computeIfAbsent, merge. saying that sentence is a legitimately strong signal because most candidates believe the collection choice finished the job.
2. "reads are cheap so i used a ReadWriteLock on the cache."
this one is my favourite because it turns a problem everyone here has solved into something uncomfortable.
LC 146, LRU cache. what does get(key) do? it looks up the node and moves it to the head of the linked list. get is a mutation. it is not a reader.
so a ReadWriteLock on an LRU cache is straightforwardly broken — you've let N threads take the read lock and concurrently relink the same doubly-linked list, which corrupts it in ways that don't show up until much later and look like data loss, not like a race. LFU (460) is the same story, the frequency bump is a write.
a read-write lock is right when reads genuinely don't touch state — "how many slots are free," "list my orders." recognising that your read path secretly writes is a senior-level observation and it costs you one sentence.
3. "python has the GIL, so i don't need locks."
self._count += 1 is load, add, store. the interpreter is allowed to switch threads between those bytecodes, so two threads can both load the same value and you lose an increment. the GIL keeps the interpreter's internals from corrupting. it says nothing about your invariants. same for the check-then-act above — the GIL will not save a two-step operation.
and the one that isn't check-then-act at all: deadlock.
wallet-to-wallet transfer. lock the sender, lock the receiver, move the money. A sends to B while B sends to A, at the same moment. thread one holds A and wants B, thread two holds B and wants A, and both of them wait forever. no error, no crash, no log line — the service just quietly stops doing transfers.
fix is one line of reasoning: always acquire locks in a stable global order, e.g. sorted by account id, so opposite transfers can never build a cycle. if you can name lock ordering unprompted you are done with this section of the interview.
related and free: don't hold a lock while you call the payment gateway. i/o inside a critical section means one slow external call serialises your entire service. take the lock, mutate state, release, then do the network call — or mark it pending inside the lock and settle outside.
what to actually say, because this is a communication round
the mistake i made after that first rejection was overcorrecting into sprinkling locks over everything, which reads as cargo cult and also tanks your throughput answer. what works is four sentences, in this order:
- name the shared mutable state. "the only thing multiple threads touch is the slot allocation map."
- name the critical section. "find-and-occupy has to be atomic together, not separately."
- name the primitive and why that one. "a lock around the allocation, and a separate read path for availability counts since those are pure reads."
- name what you gave up. "this serialises parking across all ten gates. if that's a bottleneck i'd shard the lock per floor, so gates on different floors don't contend."
that fourth sentence is the one almost nobody says and it's the one that reads as senior. it shows you know a lock is a cost, not a checkmark.
and the nuance that matters: if the problem never mentioned concurrency, don't build it. say it. "single-threaded for now — if this were multi-threaded, the slot map is where i'd put a lock and find-then-occupy is what i'd make atomic." you get the whole signal without spending fifteen minutes you needed for the demo. writing locks you weren't asked for and then not finishing the happy path is a much worse trade than saying the sentence.
how to practise it, cheaply
two things, neither of which is a big time sink:
one. after you finish any LLD design, ask one question: two threads enter this method at the same instant — which field do they both touch, and is there a check before an act on it? takes a minute. do it on parking lot, rate limiter, wallet, booking, inventory and you will have covered the realistic question space, because interviewers pull from the same short list you do.
two. actually spend one evening on the leetcode concurrency tag. 1114 through 1117, then 1188 and 1226. they are not hard problems and they are not the point. the point is that you will have typed a lock, a condition variable and a semaphore with your own hands once, so when someone asks "where would you synchronise" you're recalling something you did instead of something you read. i did them after the third rejection, in one sitting, and the difference in how fluent i sounded was out of all proportion to the effort.
the reframe
a DSA round asks whether your algorithm is correct. a machine coding round asks whether it's still correct when two people use it at the same time.
those are different questions, and the second one is never asked on leetcode, which is why passing every coding round and then failing machine coding feels so confusing from the inside. it isn't that you got worse. you got asked something the format you trained on cannot express.
the good news is how small the surface is. one bug shape, five places it hides, three false beliefs, lock ordering, and don't do i/o under a lock. that's the entire thing. it is a weekend, not a subject.
i put the long version of this on systemcraft(Google it) — a concurrency page for LLD with the locks, read-write locks, atomics and thread pools side by side in java, python and c++, plus 22 LLD problems where the ones that need it (rate limiter, wallet, task scheduler, pub-sub, inventory) actually show where the lock goes instead of hand-waving it. free, no signup to read.
what's the follow-up you got after your machine coding round was "done"? i'm collecting these. concurrency is the one i got hit with most, but the second most common for me was "how would you test this," which i also had no answer for and which is apparently a whole other post.


