Getting Ready: Traffic Signal System
Getting Ready: Traffic Signal System
Why This Problem Matters
Traffic signals govern every signalized intersection on the planet. The United States alone has over 330,000 traffic signals, while cities like London, Tokyo, and Bangalore manage tens of thousands more. Companies such as Siemens Mobility, Econolite, and Yunex Traffic build sophisticated signal controllers that must guarantee millisecond-accurate timing, handle emergency vehicle preemption, and coordinate across dozens of intersections in a corridor.
From an engineering perspective, a traffic signal controller is a timer-driven state machine with real-time constraints. Each signal head cycles through Red, Green, and Yellow (amber) states on a configurable schedule. But the complexity goes far beyond a simple three-state loop: coordinated intersections must synchronize their phases so a driver hitting green at one intersection also hits green at the next — a concept known as the "green wave." Emergency vehicles trigger preemption, which overrides the normal cycle immediately. Pedestrian crossing signals run in lockstep with vehicular phases but have their own Walk / Don't Walk states.
This is why the Traffic Signal Control System is a highly valued LLD interview question at companies like Google, Amazon, Siemens, and Bosch. It tests your ability to model concurrent state machines (multiple signal heads operating in coordinated phases), apply the State Pattern to enforce safe transitions, and use the Observer Pattern to synchronize pedestrian signals with vehicular signals. Interviewers love it because a candidate who merely draws Red → Green → Yellow has barely scratched the surface — the real design challenge lies in intersection coordination, emergency override, and extensibility for new signal types.
What You'll Learn
By the end of this design problem, you will:
- Model a timer-driven state machine — define states (Red, Green, Yellow), transitions, timing configurations, and guard conditions from real-world traffic engineering requirements
- Apply the State Pattern to make each signal state a self-contained class that handles its own timing logic and transition rules (Fundamentals S12)
- Apply the Observer Pattern to synchronize pedestrian signals with vehicular phase changes (Fundamentals S12)
- Draw a State Machine Diagram showing every signal phase, transition event, guard condition, and emergency override path
- Draw a Class Diagram deriving each class and relationship directly from requirements
- Implement the complete system in Python and Java — including signal phases, intersection coordination, emergency preemption, and a simulation driver
- Test edge cases — simultaneous emergency requests, pedestrian button press during yellow, rapid phase cycling, and coordinated intersection timing
Prerequisites
Before starting this problem, make sure you're comfortable with:
- OOP Fundamentals — classes, objects, encapsulation, and polymorphism (Fundamentals S2)
- OOP Relationships — Composition and Association; the intersection composes signal heads, and signals associate with pedestrian crossings (Fundamentals S4)
- State Pattern — how an object delegates behavior to its current state class and transitions between states. This is the central pattern of the traffic signal. If you need a refresher, review Fundamentals S12.
- Observer Pattern — how a subject notifies multiple observers of state changes. Pedestrian signals observe vehicular signal changes (Fundamentals S12).
- UML State Machine Diagrams — reading states, transitions, events, guards, and actions (Fundamentals S5)
- UML Class Diagrams — reading classes, attributes, methods, and relationships (Fundamentals S5)
You should also have completed the Vending Machine (DP6), which introduces the State Pattern in a simpler context. The Traffic Signal builds on the same pattern but adds timer-driven transitions and multi-component coordination.
Problem Scope
We will design: The core domain logic of a traffic signal control system — signal state management, timer-based phase cycling, pedestrian signal coordination, emergency vehicle preemption, and a configurable intersection controller that orchestrates multiple signal heads.
We will NOT design: Network protocols between intersections, hardware interfaces to actual signal lamps, GPS-based vehicle detection, adaptive signal timing algorithms (SCATS/SCOOT), database persistence, REST APIs, or a user interface. This is a Low Level Design (object-oriented) exercise, not a System Design (infrastructure) problem.
Difficulty: Medium — This problem is a step up from the Vending Machine (DP6) because you must coordinate multiple state machines (one per signal head) within an intersection, handle timer-based transitions (not just user-triggered events), and manage emergency override as a cross-cutting concern. The class count is moderate (8-12 classes), but the interplay between components is where the design challenge lives.
How a Real Traffic Signal Works
Before designing the software, let's understand the physical system we're modeling:
-
Signal heads — Each approach to an intersection has a signal head displaying Red, Yellow (amber), or Green. A standard four-way intersection has four vehicular signal heads (one per approach: North, South, East, West).
-
Phases and rings — Traffic engineers group compatible movements into phases. At a simple four-way intersection, North-South green runs simultaneously (one phase), then East-West green runs (another phase). Conflicting movements (North-South green and East-West green) must NEVER overlap — this is the fundamental safety invariant.
-
Timing — Each phase has configured durations: green (typically 15-60 seconds), yellow (typically 3-5 seconds), and all-red clearance (1-2 seconds where every approach shows red, allowing vehicles to clear the intersection before conflicting traffic gets green).
-
Pedestrian signals — Crosswalk signals display Walk, Flashing Don't Walk (countdown), and Steady Don't Walk. They are synchronized with vehicular phases: a pedestrian crossing is only safe when the parallel vehicular phase is green.
-
Emergency vehicle preemption (EVP) — When an emergency vehicle approaches (detected via Opticom, GPS, or siren sensors), the controller interrupts the normal cycle, gives the emergency approach a green signal, and holds all conflicting approaches on red. After the emergency vehicle clears, normal cycling resumes.
-
Fixed-time vs. actuated — Fixed-time controllers cycle through phases on a constant schedule. Actuated controllers use loop detectors or cameras to detect vehicle presence and adjust green duration dynamically. Our design will use fixed-time as the base with extensibility hooks for actuated logic.
These mechanics form the basis of every requirement and class we'll define in the next tutorial.