Leetcode #1279: Traffic Light Controlled Intersection
In this guide, we solve Leetcode #1279 Traffic Light Controlled Intersection in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
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.
Quick Facts
- Difficulty: Easy
- Premium: Yes
- Tags: Concurrency
Intuition
The constraints allow a direct scan that keeps only the essential state.
By translating the requirements into a clean loop, the logic stays easy to reason about.
Approach
Iterate through the data once, updating the state needed to compute the answer.
Return the final state after the traversal is complete.
Steps:
- Parse the input.
- Iterate and update state.
- Return the computed answer.
Example
Input: cars = [1,3,5,2,4], directions = [2,1,2,4,3], arrivalTimes = [10,20,30,40,50]
Output: [
"Car 1 Has Passed Road A In Direction 2", // Traffic light on road A is green, car 1 can cross the intersection.
"Car 3 Has Passed Road A In Direction 1", // Car 3 crosses the intersection as the light is still green.
"Car 5 Has Passed Road A In Direction 2", // Car 5 crosses the intersection as the light is still green.
"Traffic Light On Road B Is Green", // Car 2 requests green light for road B.
"Car 2 Has Passed Road B In Direction 4", // Car 2 crosses as the light is green on road B now.
"Car 4 Has Passed Road B In Direction 3" // Car 4 crosses the intersection as the light is still green.
]
Python Solution
from threading import Lock
class TrafficLight:
def __init__(self):
self.lock = Lock()
self.road = 1
def carArrived(
self,
carId: int, # ID of the car
# ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
roadId: int,
direction: int, # Direction of the car
# Use turnGreen() to turn light to green on current road
turnGreen: 'Callable[[], None]',
# Use crossCar() to make car cross the intersection
crossCar: 'Callable[[], None]',
) -> None:
self.lock.acquire()
if self.road != roadId:
self.road = roadId
turnGreen()
crossCar()
self.lock.release()
Complexity
The time complexity is O(n). The space complexity is O(1) to O(n).
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.