Shortest Path in a Grid with Obstacles Elimination
Updated
Problem statement
You are given an m x n binary grid. A value of 0 is an empty cell and a value of 1 is an obstacle. Starting at the top-left cell, you may move one step up, down, left, or right.
You may eliminate at most k obstacles. Return the minimum number of steps needed to reach the bottom-right cell, or -1 when it is impossible.
Function
shortestPath(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 = 6Eliminating one obstacle allows a shortest route of six steps.
Example 2
grid = [[0,1,1],[1,1,1],[1,0,0]]k = 1return = -1Every route to the destination requires eliminating more than one obstacle.
Constraints
1 <= m, n <= 401 <= m * ngrid[i][j]is either0or1.grid[0][0] == 0andgrid[m - 1][n - 1] == 0.0 <= k <= m * n
More Mercor interview questions
- Expected Fake-Name CollisionsPHONE SCREEN · Seen Oct 2026
- Length of a Document Reached Through a Random TokenPHONE SCREEN · Seen Oct 2026
- Precision of Stacked PII Detector FlagsPHONE SCREEN · Seen Oct 2026
- Sort by Checking PermutationsPHONE SCREEN · Seen Oct 2026
- Coffee Rank ProbabilityPHONE SCREEN · Seen Oct 2026
- CSV Row SerializerPHONE SCREEN · Seen Sep 2026
- Process Refunds with Bounded RetriesPHONE SCREEN · Seen Sep 2026
- Top K With a Deterministically Noisy Comparator (MLE)ONSITE INTERVIEW · Seen Dec 2026
- Merge Sort Report with a Noisy ComparatorONSITE INTERVIEW · Seen Sep 2026
- Minimum Friend API CallsONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Find the Safest Path in a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
- Shortest Path in a Grid with Obstacles EliminationBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Shortest Path in a Blocked GridSpotify · PHONE SCREEN
- Shortest Path in a Binary MatrixMeta · PHONE SCREENMeta interview questions
- Place a Service Table Near All CustomersGoogle · PHONE SCREENGoogle interview questions
- Shortest Path to Food with DirectionsZip · PHONE SCREENZip interview questions
- Safest Shortest Path Through a GridSnowflake · OASnowflake interview questions
- Competitive ForagingAkuna Capital · OAAkuna Capital interview questions
- Drone Delivery with Battery Swap StationsCisco · OACisco interview questions