Design a Traffic Light Controller
🚦 What is a traffic light controller? It is the small computer in the box at the corner of a crossroads. It decides which road gets green, for how long, when the light turns yellow, and when pedestrians get to walk. The lamps themselves are dumb: they show whatever the controller tells them.
This is a popular easy problem because the whole design is one state machine driven by a timer. It checks whether you can model state cleanly, handle time without a real clock, and protect the one rule that must never break: two crossing roads are never green at the same time. The pedestrian button adds just enough change to the timing to show whether your design bends or breaks.
Requirements
"Design the controller for a traffic light at a crossroads. The lights cycle on timers, and pedestrians can press a button to cross."
The prompt hides most of the decisions. These questions settle them.
Clarifying Questions
You: How many roads meet here, and are there turn arrows?
Interviewer: Two roads: north-south and east-west. Both directions of the same road share one light. No turn arrows for now.
You: What does one cycle look like?
Interviewer: North-south green, then north-south yellow, then east-west green, then east-west yellow, and around again. All greens have the same length, and so do all yellows. The lengths are configuration.
You: What does the pedestrian button do?
Interviewer: It asks for a walk phase where every car light is red and pedestrians can cross in any direction. The walk phase comes right after the next yellow, so cars always get their yellow first. One walk phase serves everybody who pressed before it started.
You: Does pressing the button make the current green shorter?
Interviewer: Yes, nobody wants to wait a whole green. But a green that just started must still run for a minimum time, so cars that just got moving are not stopped right away.
You: What should press_button give back?
Interviewer: The time the walk phase starts. The box at the corner shows a countdown.
You: Should the controller read the system clock?
Interviewer: No. Pass the time in, in seconds since the controller started. Each call's time is at least the previous one's.
The second and fifth answers matter most. The second tells us the lights are not independent: the whole crossroads is in one phase at a time. The fifth tells us we need to know the future schedule, not just the current light.
💡 Interview tip: For anything driven by time, ask "can I pass the time in?" in the first five minutes. It turns a threaded, clock-reading program into plain methods you can trace on the whiteboard, and every test becomes repeatable.
Final Requirements
- The crossroads has two roads,
NSandEW. Each road has one car light: green, yellow or red. - The phases cycle
NS_GREEN→NS_YELLOW→EW_GREEN→EW_YELLOW→NS_GREEN, starting withNS_GREENat time 0. Greens lastgreenseconds and yellowsyellowseconds. - Two roads are never green or yellow at the same time. During a road's green or yellow, the other road is red.
- A button press schedules a
WALKphase ofwalkseconds, in which both car lights are red. It starts when the next yellow ends; after it, the cycle continues with the green that yellow was leading to. - A press during a green shortens that green, but the green always runs at least
min_greenseconds. press_buttonreturns the time the walk starts (or the current time if the walk is already on).- Anyone can ask which phase is on, or what one road's light shows, at a given time.
Out of scope
- Turn arrows, more roads, and car sensors (we come back to all three in Extensibility).
- Emergency vehicles and night modes.
- Driving the actual lamps and reading a real clock.