Home
← All questions

Traffic light controller | Frontend Machine Coding

MediumPremiumMachine Coding

Asked at Google · Amazon · Microsoft

Frontend Machine Coding interview practice: Model a traffic intersection with timed state transitions

Problem

Build a traffic light UI that cycles Green → Yellow → Red with configurable durations, and supports multiple intersections in sync or offset.

Requirements

  • Single light: auto-cycle GREEN (3s) → YELLOW (1s) → RED (3s).
  • Display current color and countdown timer.
  • Buttons: pause/resume, manual override (emergency all-red).
  • (Bonus) two intersections where North-South green implies East-West red.

Hints / Approach

  • State machine: { color, remainingMs } updated via requestAnimationFrame or 100ms tick.
  • Store durations in config object; derive next state with a map.
  • Cleanup timers on unmount.
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium