Traffic Light Controlled Intersection - Problem
Traffic Light Controlled Intersection

Picture a busy intersection where two roads cross: Road A runs North-South and Road B runs West-East. Cars travel in both directions on each road:

🚗 Road A: Direction 1 (North→South), Direction 2 (South→North)
🚗 Road B: Direction 3 (West→East), Direction 4 (East→West)

Each road has a traffic light that can be either GREEN ✅ (cars can cross) or RED ❌ (cars must wait). Critical rule: Only one road can have a green light at a time!

Initially, Road A has a green light and Road B has a red light. When cars arrive, you must coordinate the traffic lights to prevent deadlock while ensuring no two cars from different roads cross simultaneously.

Your Mission: Implement carArrived(carId, roadId, direction, turnGreen, crossCar) where:
carId: unique identifier for the car
roadId: 1 for Road A, 2 for Road B
direction: travel direction (1-4)
turnGreen(): function to turn current road's light green
crossCar(): function to let current car cross

Warning: Turning a light green when it's already green is considered incorrect!

Input & Output

example_1.py — Basic Traffic Flow
$ Input: Cars arriving: [(1, 1, 1), (2, 1, 2), (3, 2, 3)] Initial state: Road A=GREEN, Road B=RED
Output: Car 1 crosses (no light change needed) Car 2 crosses (no light change needed) Road B light turns GREEN Car 3 crosses
💡 Note: Cars 1 and 2 can cross without light changes since Road A is initially green. Car 3 requires switching to Road B.
example_2.py — Alternating Roads
$ Input: Cars arriving: [(1, 2, 3), (2, 1, 1), (3, 2, 4), (4, 1, 2)]
Output: Road B light turns GREEN Car 1 crosses Road A light turns GREEN Car 2 crosses Road B light turns GREEN Car 3 crosses Road A light turns GREEN Car 4 crosses
💡 Note: Each car is from a different road than the previous, requiring light switches for optimal traffic flow.
example_3.py — Same Road Sequence
$ Input: Cars arriving: [(1, 1, 1), (2, 1, 2), (3, 1, 1), (4, 1, 2)]
Output: Car 1 crosses (no light change) Car 2 crosses (no light change) Car 3 crosses (no light change) Car 4 crosses (no light change)
💡 Note: All cars are from Road A, so no light switches are needed since Road A starts with green light.

Visualization

Tap to expand
GREENRoad AREDRoad BCar 1Road ACar 2Road BCan cross immediatelyMust wait for light changeState Tracking🟢 Current Green: Road A🔒 Mutex: UnlockedNext car will:✅ Cross if Road A car🔄 Switch if Road B carOptimal Traffic Light Control System
Understanding the Visualization
1
Car Arrives
A car approaches the intersection and requests access to cross
2
State Check
System checks if the car's road currently has the green light
3
Smart Switch
If needed, traffic light switches to car's road (avoiding unnecessary switches)
4
Safe Crossing
Car crosses the intersection safely while holding the lock
Key Takeaway
🎯 Key Insight: By tracking the current green road state and using proper synchronization, we eliminate unnecessary light switches while ensuring thread-safe operation and preventing deadlock in concurrent scenarios.

Time & Space Complexity

Time Complexity
⏱️
O(1) per car arrival

Each car operation is constant time with minimal overhead

n
2n
Linear Growth
Space Complexity
O(1)

Only stores current green road state and synchronization primitives

n
2n
Linear Space

Constraints

  • 1 ≤ carId ≤ 20
  • roadId ∈ {1, 2} where 1 = Road A, 2 = Road B
  • direction ∈ {1, 2, 3, 4} where 1,2 are Road A directions and 3,4 are Road B directions
  • Initially, Road A has GREEN light and Road B has RED light
  • At most 20 cars will call carArrived function
  • Cars may arrive concurrently from different threads
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
29.0K Views
Medium Frequency
~15 min Avg. Time
847 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