Magicsheet logo

Traffic Light Controlled Intersection

Easy
12.5%
Updated 8/1/2025

Asked by 1 Company

Traffic Light Controlled Intersection

What is this problem about?

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.

Why is this asked in interviews?

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.

Algorithmic pattern used

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.

Example explanation

  1. Initial State: North-South road has Green.
  2. Car A (North) arrives: Light is already Green for North. Car A passes.
  3. Car B (East) arrives: Light is Green for North. Car B must change the light to Green for East.
  4. Car B calls turnGreen(), then passes.
  5. Car C (North) arrives: Light is now Green for East. Car C must change the light back to Green for North. The system ensures that the light changing logic is thread-safe and that cars from different directions don't collide in the intersection.

Common mistakes candidates make

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.

Interview preparation tip

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.

Similar Questions