FastPrepFour-Direction Unique Grid Paths

Four-Direction Unique Grid Paths

Bloomberg LP logoBloomberg LP● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

In a binary grid, 1 is passable and 0 blocked. Return every simple path from the start cell to the end cell using Down, Left, Right, or Up moves. A simple path visits no cell more than once.

Encode paths with letters D, L, R, U and return them lexicographically.

Function

allGridPaths(grid: int[][], startRow: int, startCol: int, endRow: int, endCol: int) → String[]

Examples

Example 1

grid = [[1,1],[1,1]]startRow = 0startCol = 0endRow = 1endCol = 1return = ["DR","RD"]

The two simple shortest paths use Down-Right and Right-Down.

Constraints

  • At most 16 cells are passable.
  • Start and end are valid grid coordinates.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] allGridPaths(int[][] grid, int startRow, int startCol, int endRow, int endCol) {
  // Write your code here.
}
grid[[1,1],[1,1]]
startRow0
startCol0
endRow1
endCol1
expected["DR", "RD"]
Checking account…