Report Intersection Light Phases With a Pedestrian Request Button

Last updated: November 29, 2026

Quick Overview

A traffic light cycles through fixed phases such as NS_GREEN, NS_YELLOW, EW_GREEN, EW_YELLOW with given durations. A pedestrian button pressed during a green phase shortens that green so it ends at most cut seconds after the press; given sorted press times and arbitrary query times, return the phase at each query.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A traffic light cycles through fixed phases such as NS_GREEN, NS_YELLOW, EW_GREEN, EW_YELLOW with given durations. A pedestrian button pressed during a green phase shortens that green so it ends at most cut seconds after the press; given sorted press times and arbitrary query times, return the phase at each query.

A Google onsite simulation in the style of smart city or Maps live traffic modeling. The interviewer checks that you model time as phase intervals rather than ticking every second, apply events in order, and can skip ahead over long quiet periods.

What the Interviewer Expects
  • Represent the cycle as intervals with a moving end time
  • Apply presses only within the current phase and only to green phases
  • Answer queries in sorted order while keeping original positions
  • Skip whole cycles when no presses remain before the next query
  • Discuss interval boundaries: a query at exactly the end belongs to the next phase
Key Topics to Cover
simulation
event processing
state machine
two pointers
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support a minimum green time that presses cannot cut below?
  • What if presses can arrive out of order from different poles?
  • How would you coordinate a green wave across several intersections?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: phases as a list of (name, duration) repeating forever from t = 0, a sorted list presses, a list queries, and cut seconds. A phase occupies [start, end). A press at time p in a phas...

High-Level Design

Sort query indices by time. Walk phases: start, end = start + duration. First apply all presses with p < end (advancing a pointer; presses before start were already consumed). Then answer ever...


Submit Your Answer
Markdown supported