Implement a Traffic Light System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-06-02

Problem Statement

Design a low-level architecture for a traffic light system that manages vehicle flow at intersections. The system should control traffic lights, handle timing cycles, adapt to traffic conditions, and ensure safety for vehicles and pedestrians.


Functional Requirements (FR)

Traffic Light Control


•Manage standard traffic lights for multiple directions (e.g., North-South, East-West).
•Each light has states: Red, Yellow, Green.
•Cycle through lights in predefined intervals.


Pedestrian Signals


•Control pedestrian crossings with Walk and Don’t Walk signals.
•Synchronize pedestrian signals with vehicle traffic lights.


Timing & Scheduling


•Configure time intervals for each light based on traffic density.
•Support default fixed-timing cycles.
•Optionally, adapt timing dynamically based on real-time traffic.


Manual Override & Emergency Handling


•Allow traffic police or system to override default behavior.
•Give priority to emergency vehicles (ambulance, fire trucks).


Monitoring & Alerts


•Detect and alert if a light fails or is stuck.
•Log light changes for auditing.


Non-Functional Requirements (NFR)


•Reliability → Ensure traffic lights always function correctly.
•Real-Time Operation → Immediate state changes according to schedule or override.
•Scalability → Support multiple intersections in a city.
•Safety → Prevent conflicting signals to avoid accidents.
•Extensibility → Support future integration with smart traffic management systems.


Core Classes & Entities


TrafficLight


•lightId, direction, currentState (Red/Yellow/Green), duration


PedestrianSignal


•signalId, currentState (Walk/Don’t Walk), duration


Intersection


•intersectionId, trafficLights, pedestrianSignals, controller


Controller


•Responsible for managing the sequence of lights and signals.
•Can include timers, sensors, or manual overrides.


Sensor (Optional)


•Detect vehicle presence or traffic density.
•Provide data to dynamically adjust light durations.


Key Methods / Operations


TrafficLight


•changeState(newState) → change traffic light state
•getCurrentState() → retrieve current state


PedestrianSignal


•changeState(newState) → change pedestrian signal
•getCurrentState() → retrieve current state


Controller


•startCycle() → start default timing cycle
•adjustTiming(sensorData) → dynamically update durations
•manualOverride(lightId, newState) → override specific light


Intersection


•addTrafficLight(trafficLight)
•addPedestrianSignal(signal)
•run() → run the intersection light cycles


Design Considerations


Synchronization


•Ensure vehicle and pedestrian lights are coordinated to avoid conflicts.


Concurrency


•Multiple intersections can operate concurrently.
•Use threads or timers to manage state transitions.


Dynamic Adaptation


•Integrate vehicle sensors for adaptive traffic management.


Extensibility


•Support integration with city-wide traffic control systems.
•Add support for smart traffic lights or IoT-enabled devices.


Example Flow


1.Controller starts intersection cycle → North-South Green, East-West Red.
2.Timer expires → North-South Yellow → East-West Red.
3.Timer expires → North-South Red, East-West Green.
4.Pedestrian signals are synchronized with vehicle lights.
5.Sensors detect traffic → Controller adjusts next cycle dynamically.
6.Manual override triggers → Emergency vehicle priority applied.


Solution

Comments