Design a four-way traffic signal controller with timed phases and emergency overrides

Quick Overview

Design a controller for a four-way intersection that cycles each direction through green, yellow and red on a timer and supports emergency overrides. It tests state machine modeling, simulating time across phase boundaries, and edge cases such as an override in the middle of a yellow phase.

Design a four-way traffic signal controller with timed phases and emergency overrides

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a traffic signal controller for a 4-way intersection. Each direction (`North`, `South`, `East`, `West`) has a traffic light that can be in one of three states: `Red`, `Yellow` or `Green`. The controller manages light state transitions and timing logic. Only one direction can be `Green` or `Yellow` at any time, and all other directions must be `Red`. The normal cycle for a direction is `Green` to `Yellow` to `Red`. When a direction completes its `Yellow` phase, the next direction in the fixed cycle order (`North` to `South` to `East` to `West` to `North`) automatically becomes `Green`. The controller also supports emergency vehicle overrides that immediately turn a specific direction `Green` (with a full green timer reset) while setting all other directions to `Red`. After the emergency green phase completes normally, the regular cycle resumes from that direction. The system starts with all lights `Red`. Calling `startSystem` begins the cycle with `North` set to `Green`. The `greenDuration` and `yellowDuration` parameters (in seconds) are provided at construction time. Implement the following methods: - `TrafficSignalController(greenDuration: int, yellowDuration: int)`: create the controller with all lights `Red` and the clock at 0. - `startSystem()`: start the traffic system. Sets the first direction (North) to Green with a full green timer. - `advanceTime(seconds: int)`: advance the clock by the given number of seconds, transitioning lights through the Green, Yellow and Red phases as needed. - `emergencyOverride(direction: str)`: immediately set the specified direction to Green (with the full green duration) and all other directions to Red. - `getStatus() -> List[str]`: return the current light states as a list of 4 strings for [North, South, East, West]. - `getCurrentTime() -> int`: return the current internal timestamp. - `getActiveDirection() -> str`: return the name of the direction currently Green or Yellow, or `"None"` if the system has not been started. Example 1: ```text c = TrafficSignalController(5, 2) c.getStatus() -> ["Red", "Red", "Red", "Red"] c.getActiveDirection() -> "None" c.startSystem() c.getStatus() -> ["Green", "Red", "Red", "Red"] c.advanceTime(5) c.getStatus() -> ["Yellow", "Red", "Red", "Red"] c.advanceTime(2) c.getStatus() -> ["Red", "Green", "Red", "Red"] c.getActiveDirection() -> "South" c.advanceTime(10) c.getStatus() -> ["Red", "Red", "Green", "Red"] c.getCurrentTime() -> 17 ``` North is Green for seconds 0 to 5 and Yellow for 5 to 7, so after 5 seconds it has just turned Yellow and after 7 South is Green. South's green ends at 12 and its yellow at 14, when East turns Green. At time 17 East is 3 seconds into its 5-second green. Example 2: ```text c = TrafficSignalController(5, 2) c.startSystem() c.advanceTime(6) c.getStatus() -> ["Yellow", "Red", "Red", "Red"] c.emergencyOverride("East") c.getStatus() -> ["Red", "Red", "Green", "Red"] c.advanceTime(4) c.getStatus() -> ["Red", "Red", "Green", "Red"] c.advanceTime(3) c.getStatus() -> ["Red", "Red", "Red", "Green"] c.getActiveDirection() -> "West" c.getCurrentTime() -> 13 ``` At time 6 North is one second into its yellow. The override cuts that yellow short and gives East a full 5-second green starting at 6, so East is still Green at 10. East turns Yellow at 11 and Red at 13, and the cycle resumes after East with West. ```hint Pick the state Work out the smallest set of values from which all four lights can be computed. Storing the four lights separately makes it easy for them to disagree. ``` ```hint Long jumps in time One `advanceTime` call can cross many phases. Consume the elapsed seconds one phase at a time, decide what happens when time lands exactly on a phase boundary, and think about how often the whole schedule repeats. ``` ### Constraints & Assumptions - `1 <= greenDuration <= 120` - `1 <= yellowDuration <= 30` - `yellowDuration < greenDuration` - `seconds >= 1` for every `advanceTime` call - `direction` is one of `"North"`, `"South"`, `"East"`, `"West"`. - At most 1000 total method calls. - When a phase runs out exactly at the end of an `advanceTime` call, the transition has already happened when the call returns. With `greenDuration = 5`, `advanceTime(5)` after `startSystem` leaves North `Yellow`. ### Clarifying Questions to Ask - What should `emergencyOverride` do before `startSystem` has been called: start the system at that direction, or be rejected? - If the override names the direction that is already Green or Yellow, does its green timer restart? - Should a second `startSystem` call reset the cycle to North, or be ignored? - Is there an upper bound on `seconds` in a single `advanceTime` call? ### What a Strong Answer Covers - A state model (active direction, phase, time left in the phase) that makes the "only one non-Red direction" rule hold by construction. - An `advanceTime` that crosses any number of phases, with a clear rule for landing exactly on a boundary. - Override behavior: a full green reset, all other directions Red, the cycle resuming from the overridden direction, and overrides during Yellow or before start. - Behavior before `startSystem`: the clock moves, all lights stay Red, and the active direction is `"None"`. - The cost of `advanceTime` for very large jumps and how the repeating schedule bounds it. ### Follow-up Questions - The override sends the interrupted direction straight to Red. How would you add a yellow and an all-red clearance interval before the emergency green, and what would change in the state model? - Many intersections give opposing directions green at the same time. How would you change the model to run phase groups such as North with South? - Two emergency overrides from different directions arrive a few seconds apart. What should the controller do, and how would you enforce it? - How would you test `advanceTime` across long jumps and exact phase boundaries?

Overview: Design a controller for a four-way intersection that cycles each direction through green, yellow and red on a timer and supports emergency overrides. It tests state machine modeling, simulating time across phase boundaries, and edge cases such as an override in the middle of a yellow phase.

Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a traffic signal controller for a 4-way intersection. Each direction (North, South, East, West) has a traffic light that can be in one of three states: Red, Yellow or Green.

The controller manages light state transitions and timing logic. Only one direction can be Green or Yellow at any time, and all other directions must be Red. The normal cycle for a direction is Green to Yellow to Red. When a direction completes its Yellow phase, the next direction in the fixed cycle order (North to South to East to West to North) automatically becomes Green.

The controller also supports emergency vehicle overrides that immediately turn a specific direction Green (with a full green timer reset) while setting all other directions to Red. After the emergency green phase completes normally, the regular cycle resumes from that direction.

The system starts with all lights Red. Calling startSystem begins the cycle with North set to Green. The greenDuration and yellowDuration parameters (in seconds) are provided at construction time.

Implement the following methods:

  • TrafficSignalController(greenDuration: int, yellowDuration: int) : create the controller with all lights Red and the clock at 0.
  • startSystem() : start the traffic system. Sets the first direction (North) to Green with a full green timer.
  • advanceTime(seconds: int) : advance the clock by the given number of seconds, transitioning lights through the Green, Yellow and Red phases as needed.
  • emergencyOverride(direction: str) : immediately set the specified direction to Green (with the full green duration) and all other directions to Red.
  • getStatus() -> List[str] : return the current light states as a list of 4 strings for [North, South, East, West].
  • getCurrentTime() -> int : return the current internal timestamp.
  • getActiveDirection() -> str : return the name of the direction currently Green or Yellow, or "None" if the system has not been started.

Example 1:

c = TrafficSignalController(5, 2)
c.getStatus()            -> ["Red", "Red", "Red", "Red"]
c.getActiveDirection()   -> "None"
c.startSystem()
c.getStatus()            -> ["Green", "Red", "Red", "Red"]
c.advanceTime(5)
c.getStatus()            -> ["Yellow", "Red", "Red", "Red"]
c.advanceTime(2)
c.getStatus()            -> ["Red", "Green", "Red", "Red"]
c.getActiveDirection()   -> "South"
c.advanceTime(10)
c.getStatus()            -> ["Red", "Red", "Green", "Red"]
c.getCurrentTime()       -> 17

North is Green for seconds 0 to 5 and Yellow for 5 to 7, so after 5 seconds it has just turned Yellow and after 7 South is Green. South's green ends at 12 and its yellow at 14, when East turns Green. At time 17 East is 3 seconds into its 5-second green.

Example 2:

c = TrafficSignalController(5, 2)
c.startSystem()
c.advanceTime(6)
c.getStatus()            -> ["Yellow", "Red", "Red", "Red"]
c.emergencyOverride("East")
c.getStatus()            -> ["Red", "Red", "Green", "Red"]
c.advanceTime(4)
c.getStatus()            -> ["Red", "Red", "Green", "Red"]
c.advanceTime(3)
c.getStatus()            -> ["Red", "Red", "Red", "Green"]
c.getActiveDirection()   -> "West"
c.getCurrentTime()       -> 13

At time 6 North is one second into its yellow. The override cuts that yellow short and gives East a full 5-second green starting at 6, so East is still Green at 10. East turns Yellow at 11 and Red at 13, and the cycle resumes after East with West.

Constraints & Assumptions

  • 1 <= greenDuration <= 120
  • 1 <= yellowDuration <= 30
  • yellowDuration < greenDuration
  • seconds >= 1 for every advanceTime call
  • direction is one of "North" , "South" , "East" , "West" .
  • At most 1000 total method calls.
  • When a phase runs out exactly at the end of an advanceTime call, the transition has already happened when the call returns. With greenDuration = 5 , advanceTime(5) after startSystem leaves North Yellow .

Clarifying Questions to Ask Guidance

  • What should emergencyOverride do before startSystem has been called: start the system at that direction, or be rejected?
  • If the override names the direction that is already Green or Yellow, does its green timer restart?
  • Should a second startSystem call reset the cycle to North, or be ignored?
  • Is there an upper bound on seconds in a single advanceTime call?

What a Strong Answer Covers Guidance

  • A state model (active direction, phase, time left in the phase) that makes the "only one non-Red direction" rule hold by construction.
  • An advanceTime that crosses any number of phases, with a clear rule for landing exactly on a boundary.
  • Override behavior: a full green reset, all other directions Red, the cycle resuming from the overridden direction, and overrides during Yellow or before start.
  • Behavior before startSystem : the clock moves, all lights stay Red, and the active direction is "None" .
  • The cost of advanceTime for very large jumps and how the repeating schedule bounds it.

Follow-up Questions Guidance

  • The override sends the interrupted direction straight to Red. How would you add a yellow and an all-red clearance interval before the emergency green, and what would change in the state model?
  • Many intersections give opposing directions green at the same time. How would you change the model to run phase groups such as North with South?
  • Two emergency overrides from different directions arrive a few seconds apart. What should the controller do, and how would you enforce it?
  • How would you test advanceTime across long jumps and exact phase boundaries?