Shortest Grid Path with Obstacle Elimination
Updated
Problem statement
You are given an m x n binary grid. A value of 0 is an open cell, and a value of 1 is an obstacle.
Start at the top-left cell (0, 0). In one step, you may move one cell up, down, left, or right while remaining inside the grid. You may eliminate at most k obstacles by entering their cells.
Return the minimum number of steps needed to reach the bottom-right cell (m - 1, n - 1). Return -1 when no valid path exists.
Function
shortestPathWithObstacleEliminations(grid: int[][], k: int) → intExamples
Example 1
grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]]k = 1return = 6A shortest route uses six steps and eliminates one obstacle.
Example 2
grid = [[0,1,1],[1,1,1],[1,0,0]]k = 1return = -1Every route to the destination enters more than one obstacle, so the elimination budget is insufficient.
Example 3
grid = [[0]]k = 0return = 0The start is already the destination, so no step is needed.
Constraints
1 <= m, n <= 40grid[i][j]is0or1.grid[0][0] = grid[m - 1][n - 1] = 00 <= k <= m * n
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Shortest Grid Path with Obstacle EliminationsAmazon · ONSITE INTERVIEW
- Shortest Path in a Grid with Obstacles EliminationBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Safest Shortest Path Through a GridSnowflake · OASnowflake interview questions
- Competitive ForagingAkuna Capital · OAAkuna Capital interview questions
- Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
- Shortest Path in a Grid with Obstacles EliminationMercor · PHONE SCREENMercor interview questions
- Drone Delivery with Battery Swap StationsCisco · OACisco interview questions