Nearest Other Exit on the Board Edge
Updated
Problem statement
A rectangular board contains 0 for passable cells and + for obstacles. Given a passable entry cell [startRow,startCol] on the board edge, return the closest other reachable exit on the board edge.
For this exercise, assume one move steps up, down, left or right to a passable cell and costs one unit. An exit is any passable cell in the first or last row or column, except the entry itself. Choose a reachable exit using the fewest moves. Among equally close exits, choose the smallest row index, then the smallest column index.
For this exercise, assume coordinates are zero-based and the result is [row,col]. Return [-1,-1] when there is no other reachable exit. The board is not modified.
Function
nearestEdgeExit(grid: String[], startRow: int, startCol: int) → int[]Examples
Example 1
grid = ["+0+","000","+0+"]startRow = 0startCol = 1return = [1,0]The two side exits are both two moves away; row/column order chooses [1,0].
Example 2
grid = ["+0+++","+000+","+++0+","+++0+"]startRow = 0startCol = 1return = [3,3]The only other reachable edge cell is [3,3], reached in five orthogonal moves.
Example 3
grid = ["0"]startRow = 0startCol = 0return = [-1,-1]The entry itself is excluded, so this board has no other exit.
Constraints
- For this exercise, assume
1 <= grid.length <= 30and each row has the same length from1through30. - Every cell is
0or+. - For this exercise, assume the entry indices are in range, the entry contains
0, and it lies on the first or last row or column.
More Walmart Phone Screen questions
- Count Months Starting on SundayPHONE SCREEN · Seen Aug 2026
- Longest Even-Length WordPHONE SCREEN · Seen Aug 2026
- Range ModulePHONE SCREEN · Seen Aug 2026
- Meeting Rooms IIPHONE SCREEN · Seen Aug 2026
- Sort Real Number StringsPHONE SCREEN · Seen Jan 2026
- Check a Password Against a Supplied PolicyPHONE SCREEN · Seen Jun 2025
- Convert a Restricted Function to an Arrow FunctionPHONE SCREEN · Seen Jun 2025
- Repair One Missing Function Punctuation CharacterPHONE SCREEN · Seen Jun 2025
- Can the Singer Perform the Song?PHONE SCREEN · Seen Mar 2025
- Rooms With Two Other Entrances and a Treasure SuccessorPHONE SCREEN · Seen Mar 2025
Similar questions at other companies
- Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
- Matrix EscapeByteDance · PHONE SCREENByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Word Chain Jumps in a GridGoogle · PHONE SCREENGoogle interview questions
- Grid Infection with Recovery After D DaysOpenAI · PHONE SCREENOpenAI interview questions
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Shortest Path in a Blocked GridSpotify · PHONE SCREEN
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
- Days Until Poisoned Plants StabilizeApple · PHONE SCREENApple interview questions