Nearest Reachable Grid Corner
Updated
Problem statement
You are given a non-empty rectangular grid where 0 is passable and + is impassable. The passable starting cell (startRow, startCol) is one of the grid's geometric corners.
Moving one step up, down, left, or right through passable cells costs one. Find a different passable geometric corner reachable with minimum path distance. If several destinations have the same minimum distance, return the lexicographically smallest coordinate: smallest row, then smallest column.
Return the destination as [row, column], or [-1] if no different corner is reachable. Repeated coordinates in a one-row or one-column grid represent one geometric corner.
Function
nearestReachableCorner(grid: String[], startRow: int, startCol: int) → int[]Examples
Example 1
grid = ["000","+0+","000"]startRow = 0startCol = 0return = [0,2]The top-right corner is reachable in two steps, while the bottom-left and bottom-right corners require longer paths.
Example 2
grid = ["0+","++"]startRow = 0startCol = 0return = [-1]Every other geometric corner is impassable, so no destination is reachable.
Constraints
1 <= grid.length.1 <= grid[i].length.- Every row has the same length and the grid contains at most
4,096cells. - Every cell is
0or+. (startRow, startCol)is a geometric corner and is passable.
More Splunk Phone Screen questions
Similar questions at other companies
- Matrix EscapeByteDance · PHONE SCREENByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot 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