Reachable Exits in an Obstacle Grid
Updated
Problem statement
You are given an n x n grid grid where 0 is an open cell and 1 is an obstacle, and a starting cell start = [row, col]. Return every exit you can reach from start, together with the fewest moves needed to reach it.
In one move you go one cell up, down, left or right into an open cell. You cannot move onto an obstacle or leave the grid.
An exit is an open cell on the outer edge of the grid (row 0, row n - 1, column 0 or column n - 1). The start cell is never an exit, even when it is on the edge.
Return a list of [row, col, steps] entries, one for each reachable exit, where steps is the length of the shortest path from start to that exit. Sort the list by row, then by col. If no exit can be reached, return an empty list.
Function
reachableExits(grid: int[][], start: int[]) → int[][]Examples
Example 1
grid = [[0,0,1,0,0],[0,1,0,0,1],[0,0,0,1,0],[1,1,0,1,0],[0,0,0,0,1]]start = [2,2]return = [[0,0,4],[0,1,5],[0,3,3],[0,4,4],[1,0,3],[2,0,2],[4,0,4],[4,1,3],[4,2,2],[4,3,3]]Ten exits can be reached. [2,0] is two moves left of the start. [0,1] needs five moves because the obstacles at [0,2] and [1,1] force the path around through [0,0]. The open edge cells [2,4] and [3,4] are walled off, so they are not listed.
Example 2
grid = [[1,0,1],[1,0,1],[1,1,1]]start = [0,1]return = []The start [0,1] is on the edge, but it is not an exit. The only other open cell is [1,1], which is not on the edge, so no exit can be reached.
Example 3
grid = [[1,1,1,1],[1,0,0,1],[1,0,0,1],[1,1,1,1]]start = [1,1]return = []Every edge cell is an obstacle, so there are no exits.
Constraints
n == grid.length == grid[i].length1 <= n <= 100grid[i][j]is0or1.start.length == 20 <= start[0], start[1] < ngrid[start[0]][start[1]] == 0
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE SCREEN · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Knight Dialer SequencesPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Fuel-Aware Grid ReachabilityBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Bounds in a Ragged GridRippling · OA · PHONE SCREENRippling interview questions
- Nearest Reachable Grid CornerSplunk · PHONE SCREENSplunk interview questions
- Rotting Orange PropagationIndeed · PHONE SCREENIndeed interview questions