The "Traffic Light Controlled Intersection coding problem" is a concurrency and synchronization challenge. You are asked to implement a system that controls cars passing through an intersection. There is only one road available at a time (e.g., North-South or East-West). Multiple cars (represented as threads) arrive at the intersection and call a function to pass. You must ensure that cars from the same road can pass together, but cars from a different road must wait until the traffic light changes.
This "Traffic Light Controlled Intersection interview question" is often used by companies like Amazon to evaluate a candidate's understanding of multi-threading and synchronization primitives. It tests your ability to use locks, semaphores, or monitors to manage access to a shared resource (the intersection). It's a practical application of the "Readers-Writers" problem or "Mutual Exclusion" where the state of the system (the light color) determines who can proceed.
The "Concurrency interview pattern" is the core here. You typically use a Lock (like a ReentrantLock in Java or a mutex in C++) to ensure that the process of checking the light and changing it is atomic. A boolean variable or an enum can represent the current road that has the green light. When a car arrives, it checks if its road has the green light; if not, it triggers a light change. Crucially, only one thread should be allowed to change the light at a time.
turnGreen(), then passes.One major mistake in the "Traffic Light Controlled Intersection coding problem" is not using synchronization, which leads to "race conditions" where two cars from different directions might think the light is green for them simultaneously. Another error is over-synchronizing, which can cause "deadlocks" or unnecessary delays where cars have to wait even when the road is clear. Failing to update the state of the light correctly after a car passes is also a frequent issue.
To master the "Concurrency interview pattern," practice using synchronized blocks or Locks in your preferred language. Understand the difference between "Mutual Exclusion" (preventing others) and "Condition Variables" (waiting for a state). Designing thread-safe classes with clear state transitions is a vital skill for any backend or systems engineer.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Print in Order | Easy | Solve | |
| Fizz Buzz Multithreaded | Medium | Solve | |
| Building H2O | Medium | Solve | |
| Design Bounded Blocking Queue | Medium | Solve | |
| Print Zero Even Odd | Medium | Solve |