Design a traffic light control system
I was asked this
No reports yet. Sign in to say where you were asked it.
An object-oriented design interview question asked in Amazon's technical phone screen, last asked Sep 2025. It tests object-oriented design.
Design the control software for one road intersection's traffic lights. The lights cycle through phases with set timings, pedestrians can press a button to request a crossing, and an emergency vehicle can override the normal cycle. Model it as a clear state machine that never shows green to conflicting directions.
Requirements
- Functional: run a fixed cycle of phases (for example north-south green, north-south yellow, all red, east-west green, east-west yellow, all red) with configurable durations; serve pedestrian requests by adding a walk phase at the next safe point; on an emergency signal, move safely to green for the emergency vehicle's direction and hold it, then resume the normal cycle; go to flashing red if a fault is detected.
- Non-functional: safety first: conflicting directions are never green at the same time, and every change from green passes through yellow and a short all-red; timings are configuration; the logic is testable with a simulated clock.
Clarifying questions
- How many approaches and lanes? Are there turn arrows?
- Are timings fixed, or do sensors extend green when traffic is waiting?
- How does an emergency vehicle signal its direction, and what if two arrive from different directions?
- What should happen after a power failure or a stuck light?
- Is the controller for one intersection, or coordinated with neighbors?
What a strong answer covers
- Core classes:
Intersection(approaches, signals),Signal(a light's current color),Phase(which signals are green, its duration),PhasePlan(the ordered cycle),Controller(the state machine and timer),PedestrianButton,EmergencyDetector, and aClockinterface. - The state pattern for phases, with transitions driven by timer events and requests, and the yellow and all-red clearance enforced inside the transition, not left to callers.
- A conflict matrix checked whenever a phase is defined, so an unsafe plan cannot be loaded.
- Pedestrian requests queued and served at the next phase boundary; emergency override as a higher-priority mode that still passes through yellow and all-red.
- Fault handling: falling back to flashing red.
- Tests with a fake clock that step through a cycle, a pedestrian request mid-green and an emergency during yellow.
Follow-ups
- Add sensors that extend green while cars keep arriving, up to a maximum.
- Coordinate several intersections so a main road gets a green wave.
- Add left-turn arrows and the conflicts they create.
Where it's asked
- Amazon · Technical phone screen · Last asked Sep 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
