FastPrepCyclic Fuel Command Simulation

Cyclic Fuel Command Simulation

Waymo logoWaymo● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

A vehicle starts at S in a rectangular grid. # is blocked, G is a fuel station, and reaching any cell in the final column succeeds.

Repeat program cyclically for at most maxSteps. Commands U,D,L,R move one cell and consume one fuel. F adds refillAmount fuel and is valid only while on G. Return the first successful step, 0 if already at the goal, or -1 if a command fails or the bound expires.

Function

cyclicProgramSteps(grid: String[], program: String, initialFuel: int, refillAmount: int, maxSteps: int) → int

Examples

Example 1

grid = ["S.."]program = "R"initialFuel = 2refillAmount = 3maxSteps = 5return = 2

Repeating R reaches the final column in two moves.

Example 2

grid = ["SG.."]program = "RFR"initialFuel = 1refillAmount = 2maxSteps = 6return = 4

The vehicle moves onto G, refuels, advances once, then the next cycle reaches the last column.

Example 3

grid = ["S#.","..."]program = "RDL"initialFuel = 5refillAmount = 2maxSteps = 10return = -1

The first command attempts to enter an obstacle.

Constraints

  • 1 <= rows, columns <= 200.
  • 1 <= program.length <= 10 and it contains only UDLRF.
  • 0 <= initialFuel, refillAmount, maxSteps <= 10^6.
  • The grid contains exactly one S.

More Waymo problems

See Waymo hiring insights
public int cyclicProgramSteps(String[] grid, String program, int initialFuel, int refillAmount, int maxSteps) {
    // Write your solution here.
}
grid["S.."]
program"R"
initialFuel2
refillAmount3
maxSteps5
expected2
Checking account…