FastPrepNearest Other Exit on the Board Edge
Problems/Nearest Other Exit on the Board Edge

Nearest Other Exit on the Board Edge

Walmart logoWalmart● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The closest-exit follow-up matches the named firsthand report at about 70%. The report does not give the full movement or result rules. Moves of one cell up, down, left or right, any other boundary cell counting as an exit, the row and column order for ties, a failure marker, typed inputs and finite limits are our choices. We wrote all the complete examples.

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]
grid
0120+0+10002+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]
grid
012340+0+++1+000+2+++0+3+++0+

The only other reachable edge cell is [3,3], reached in five orthogonal moves.

Example 3

grid = ["0"]startRow = 0startCol = 0return = [-1,-1]
grid
000

The entry itself is excluded, so this board has no other exit.

Constraints

  • For this exercise, assume 1 <= grid.length <= 30 and each row has the same length from 1 through 30.
  • Every cell is 0 or +.
  • 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
All Walmart interview questionsSee Walmart hiring insights
Saved
class Solution {
    public int[] nearestEdgeExit(String[] grid, int startRow, int startCol) {
        // Write your code here.
        return new int[]{-1,-1};
    }
}
Checking account…