Design a Traffic-Light Intersection
🚦 What is a traffic-light intersection controller? It is the code that decides which cars may drive through a crossing. Two roads meet, one light shows green for one road at a time, and cars keep arriving from all four directions. The controller makes sure a car only drives on green, switches the light when a car is waiting on red, and never lets cars from both roads into the crossing at once.
This is one of the friendliest concurrency questions you can get. The prompt is short and the final code is only a few lines, but every line is there for a reason. Your interviewer wants to see that you can spot a race in plain-looking code, pick the smallest lock that fixes it, and explain why the result cannot deadlock. It is also a good warm-up for harder problems with the same shape, such as the unisex bathroom and dining philosophers.
Requirements
"Two roads cross at an intersection with one traffic light. Cars arrive from four directions, and each car runs on its own thread. Make sure every car gets through safely."
The questions below turn that sentence into rules we can code against.
Clarifying Questions
You: How are the four directions grouped into roads?
Interviewer: Road 1 carries directions 1 and 2 (north and south). Road 2 carries directions 3 and 4 (east and west). The light is green for exactly one road at a time, and it starts green for road 1.
You: Who changes the light? Is there a timer?
Interviewer: No timer. A car that arrives on red switches the light to its own road by calling a turnGreen function we give you. Don't call it if the light is already green for that road.
You: How does a car actually cross in code?
Interviewer: You also get a crossCar function. Call it once per car; it returns when the car is through. Cars must only cross on green.
You: Can two cars from the same road be in the intersection together?
Interviewer: The one hard rule is that cars from different roads are never in there at the same time. Letting one car through at a time is fine for now. We may talk about throughput later.
You: May the light change while a car is still crossing?
Interviewer: No. That is how accidents happen.
You: Do we need to be fair, so one road cannot starve the other?
Interviewer: Not for the first version. Keep it correct and simple.
The fourth answer is the one that keeps this problem easy. "One car at a time" lets a single lock do all the work. If the interviewer had asked for cars on the green road to share the intersection, we would need a counter and a condition variable, and we would have to think about fairness. We will build that version too, but only after the simple one works.
💡 Interview tip: In concurrency problems, ask what may happen at the same time before you ask anything else. "Never two roads at once" and "never two cars at once" sound alike but lead to different designs. Pin down which one is required and which one is merely allowed.
Final Requirements
- Two roads: road 1 (directions 1 and 2) and road 2 (directions 3 and 4). The light is green for one road at a time and starts green for road 1.
- Each car runs on its own thread and calls
car_arrived(car_id, road_id, direction, turn_green, cross_car). - A car crosses (calls
cross_car) exactly once, and only while the light is green for its road. - If the light is red for the car's road, the car calls
turn_greenfirst. It never callsturn_greenwhen its road is already green. - Cars from the two roads are never in the intersection at the same time, and the light never changes while a car is crossing.
- The controller never deadlocks: every call to
car_arrivedreturns.
Out of scope
- A timed light cycle (we come back to this in Extensibility).
- Turn arrows, pedestrian crossings and emergency vehicles.
- Fairness between the roads in the first version.
- The real hardware: we only call the two functions we are handed.