Traffic Light Controlled Intersection - Problem

There is an intersection of two roads. Road A allows cars to travel from North to South in direction 1 and from South to North in direction 2. Road B allows cars to travel from West to East in direction 3 and from East to West in direction 4.

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

The traffic lights cannot be green on both roads at the same time. Initially, the traffic light is green on road A and red on road B.

Design a deadlock-free traffic light controlled system. Implement the function carArrived(carId, roadId, direction, turnGreen, crossCar) where:

  • carId is the id of the car that arrived
  • roadId is the id of the road (1 for road A, 2 for road B)
  • direction is the direction of the car
  • turnGreen is a function to turn the traffic light green on the current road
  • crossCar is a function to let the current car cross the intersection

Constraints: Your solution must avoid deadlock and ensure only cars from the same road cross simultaneously.

Input & Output

Example 1 — Basic Car Flow
$ Input: Car 1 arrives on road A (direction 1), Car 2 arrives on road A (direction 2)
Output: Both cars cross since road A is initially green
💡 Note: Road A starts with green light. Both cars from road A can cross without switching lights.
Example 2 — Light Switch Required
$ Input: Car 1 on road A crosses, then Car 2 arrives on road B (direction 3)
Output: Car 2 turns light green for road B, then crosses
💡 Note: Car 2 needs to switch from road A (green) to road B (green), making road A red.
Example 3 — Multiple Cars Same Road
$ Input: Car 1, Car 2, Car 3 all arrive on road B simultaneously
Output: Light switches to road B, all three cars cross together
💡 Note: Multiple cars from same road can cross simultaneously once that road has green light.

Constraints

  • 1 ≤ carId ≤ 1000
  • roadId ∈ {1, 2} (1 for road A, 2 for road B)
  • direction ∈ {1, 2, 3, 4}
  • Initially road A is green, road B is red
  • Only one road can be green at any time

Visualization

Tap to expand
Traffic Light Controlled IntersectionIntersectionARoad A - GREENBRoad B - REDCar N→SCar S→NCar W→ECar E→WDirection 1: N→SDirection 2: S→NDirection 3: W→EDirection 4: E→W🎯 Key: Only one road can be green at a time
Understanding the Visualization
1
Initial State
Road A green, Road B red, cars can arrive on either road
2
Light Control
Switch lights when car from different road needs to cross
3
Safe Crossing
Cars cross intersection without collision
Key Takeaway
🎯 Key Insight: Use mutex locks to ensure mutual exclusion between roads
Asked in
Google 15 Meta 12 Amazon 8
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen