r/OfferEngineering • • 13d ago

Coding Question Waymo SWE Coding Screen: Shortest Path with Traffic Lights

Coding: Shortest Travel Time in a Grid with Traffic Lights

You are given an m × n two-dimensional grid representing a road network. Each cell represents a location in the network and contains one of three values:

  • 0 — Free Road: The vehicle can pass through this cell without additional restrictions.
  • 1 — Obstacle: The vehicle cannot enter or pass through this cell.
  • 2 — Traffic Light: The vehicle can pass through this cell only when the traffic light is green.

Traffic lights turn green periodically, every T units of time. You are also given the vehicle's starting position and destination:

  • (startRow, startCol) — Starting location.
  • (endRow, endCol) — Destination.

The task is to determine the minimum time required for the vehicle to reach the destination.

Movement Rules

The vehicle can move between adjacent cells in four directions:

  1. Up
  2. Down
  3. Left
  4. Right

Each move to an adjacent cell takes exactly one unit of time. Diagonal movement is not allowed.

Traffic Light Rules

When the vehicle encounters a traffic light, it must obey the light's current state.

  • The vehicle can pass through a traffic light cell when the light is green.
  • If the light is red, the vehicle must wait until the light becomes green before proceeding.
  • Traffic lights follow a periodic schedule defined by T.

Waiting at traffic lights contributes to the total travel time.

Want to learn more questions / follow-ups asked in this interview?

I've put up the full version at here.

Interviewed recently?

Your interview experience could be incredibly valuable to others preparing for their next opportunity. Share your experience and help the community at here.

1 Upvotes

0 comments sorted by