FastPrepMatrix Path Existence

Matrix Path Existence

Wells Fargo logoWells Fargo● MediumINTERNOA
Learn

Problem statement

Given a rectangular binary matrix grid, determine whether a path exists from the top-left cell to the bottom-right cell.

  • A cell containing 1 is passable.
  • A cell containing 0 is blocked.
  • You may move one cell at a time up, down, left, or right.

Return true when such a path exists and false otherwise.

Function

pathExists(grid: int[][]) → boolean

Examples

Example 1

grid = [[1,1,0],[0,1,1],[0,0,1]]return = true

A path follows the passable cells from the top-left cell through the center to the bottom-right cell.

Example 2

grid = [[1,0],[0,1]]return = false

Diagonal movement is not allowed, so the two passable cells are disconnected.

Example 3

grid = [[1]]return = true

The single cell is both the start and the destination.

Example 4

grid = [[0,1],[1,1]]return = false

The starting cell is blocked.

Constraints

  • 1 <= grid.length <= 200.
  • 1 <= grid[i].length <= 200.
  • Every row has the same length.
  • grid[i][j] is 0 or 1.

More Wells Fargo problems

See Wells Fargo hiring insights
public boolean pathExists(int[][] grid) {
  // write your code here
}
grid[[1,1,0],[0,1,1],[0,0,1]]
expectedtrue
Checking account…