FastPrepShortest Path in a Grid with Obstacles Elimination
Problems/Shortest Path in a Grid with Obstacles Elimination

Shortest Path in a Grid with Obstacles Elimination

Mercor logoMercor● HardFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Mercor phone screen. Our version is about 99% the same as the reported question.

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) → int

Examples

Example 1

grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]]k = 1return = 6
grid
01200001110200030114000

Eliminating one obstacle allows a shortest route of six steps.

Example 2

grid = [[0,1,1],[1,1,1],[1,0,0]]k = 1return = -1
grid
012001111112100

Every route to the destination requires eliminating more than one obstacle.

Constraints

  • 1 <= m, n <= 40
  • 1 <= m * n
  • grid[i][j] is either 0 or 1.
  • grid[0][0] == 0 and grid[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

Mercor interview experiences

  • Mercor New Grad Research Engineer Interviews: 1 Experience
All Mercor interview questionsSee Mercor hiring insights
Saved
class Solution {
    public int shortestPath(int[][] grid, int k) {
        // Write your code here
    }
}
Checking account…