← Questions

Design a traffic light control system

0
AmazonOO designLast asked Sep 2025

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 a Clock interface.
  • 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

object-oriented design

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing