Shortest Path Between Grid Points
Updated
Problem statement
Given a rectangular binary matrix grid and two open cells (startRow, startCol) and (targetRow, targetCol), return the minimum number of moves needed to reach the target from the start.
From an open cell, you may move one cell up, down, left, or right. A cell containing 0 is open and a cell containing 1 is blocked. You may not enter blocked cells. Return -1 if the target is unreachable.
Function
shortestGridPath(grid: int[][], startRow: int, startCol: int, targetRow: int, targetCol: int) → intExamples
Example 1
grid = [[0,0,0],[1,1,0],[0,0,0]]startRow = 0startCol = 0targetRow = 2targetCol = 2return = 4Move right twice and then down twice. The blocked middle-row cells prevent a shorter route.
Example 2
grid = [[0,1],[1,0]]startRow = 0startCol = 0targetRow = 1targetCol = 1return = -1Both possible first moves are blocked.
Example 3
grid = [[0]]startRow = 0startCol = 0targetRow = 0targetCol = 0return = 0The start is already the target, so no move is needed.
Constraints
1 <= grid.length, grid[0].length <= 500- Every row has the same length, and every cell is
0or1. - The start and target coordinates are inside the grid and refer to open cells.
- The total number of cells is at most
250000.
More Bloomberg LP Onsite Interview questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Number of IslandsFleetx · ONSITE INTERVIEW
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
- Rotting OrangesSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Minimum Time to Rot All OrangesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle 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
- Rotting OrangesOtter.ai · ONSITE INTERVIEWOtter.ai interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)