FastPrepFuel-Aware Grid Reachability

Fuel-Aware Grid Reachability

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

You have a tank whose capacity is fuelCapacity. It starts full. The rectangular grid contains exactly one start cell S, exactly one target cell T, open ground ., blocked cells #, and fuel cells G.

You may move one cell up, down, left, or right. Each move consumes one unit of fuel, so you may leave a cell only when at least one unit remains. You cannot enter a blocked cell. Entering a fuel cell immediately restores the tank to fuelCapacity; a fuel cell may refill the tank again on a later visit.

Return true if you can reach T, and return false otherwise. Reaching T with zero fuel remaining is allowed.

Function

canReachWithFuel(grid: String[], fuelCapacity: int) → boolean

Examples

Example 1

grid = ["S..",".#G","...","..T"]fuelCapacity = 4return = true

The start-to-target distance exceeds four moves, but the path can reach G in three moves, refill to four units, and then reach T.

Example 2

grid = ["S#T","..."]fuelCapacity = 3return = false

The obstacle forces a four-move detour, and there is no fuel cell to extend the initial three units.

Example 3

grid = ["SGT"]fuelCapacity = 1return = true

The first move spends the only fuel unit, entering G restores it, and the second move reaches T.

Constraints

  • 1 <= grid.length <= 200.
  • 1 <= grid[i].length <= 200, and all rows have the same length.
  • 0 <= fuelCapacity <= 400.
  • Every cell is one of S, T, ., #, or G.
  • The grid contains exactly one S and exactly one T.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public boolean canReachWithFuel(String[] grid, int fuelCapacity) {
    // Write your code here.
}
grid["S..",".#G","...","..T"]
fuelCapacity4
expectedtrue
Checking account…