Skip to main content
LeetCode 1279, Easy. Topics: Concurrency, Design. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

Problem

There is an intersection of two roads. First road is road A where cars travel from North to South in direction 1 and from South to North in direction 2. Second road is road B where cars travel from West to East in direction 3 and from East to West in direction 4. Example There is a traffic light located on each road before the intersection. A traffic light can either be green or red.
  • Green means cars can cross the intersection in both directions of the road.
  • Red means cars in both directions cannot cross the intersection and must wait until the light turns green.
The traffic lights cannot be green on both roads at the same time. That means when the light is green on road A, it is red on road B and when the light is green on road B, it is red on road A. Initially, the traffic light is green on road A and red on road B. When the light is green on one road, all cars can cross the intersection in both directions until the light becomes green on the other road. No two cars traveling on different roads should cross at the same time. Design a deadlock-free traffic light controlled system at this intersection. Implement the function car_arrived(car_id, road_id, direction, turn_green, cross_car) where:
  • car_id is the id of the car that arrived.
  • road_id is the id of the road that the car travels on. Can be 1 (road A) or 2 (road B).
  • direction is the direction of the car.
  • turn_green is a function you can call to turn the traffic light to green on the current road.
  • cross_car is a function you can call to let the current car cross the intersection.

Examples

Constraints

  • 1 <= cars.length <= 20
  • cars.length = directions.length
  • cars.length = arrivalTimes.length
  • All values of cars are unique
  • 1 <= directions[i] <= 4
  • arrivalTimes is non-decreasing
Your answer is considered correct if it avoids cars deadlock in the intersection. Turning the light green on a road when it was already green is considered a wrong answer.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026