FastPrepPath Through an O/X Grid Using Only Right and Down

Path Through an O/X Grid Using Only Right and Down

Amazon logoAmazonâ—Ź MediumFULLTIMEPHONE SCREEN

Problem statement

Given a rectangular array of strings grid, a zero-based coordinate source = [row, col], and a zero-based coordinate destination = [row, col], return whether the destination is reachable.

The character O is open and X is blocked. From an open cell you may move exactly one cell right or one cell down. A blocked endpoint is unreachable, and a destination above or left of the source is also unreachable.

Function

hasRightDownPath(grid: String[], source: int[], destination: int[]) → boolean

Examples

Example 1

grid = ["OOO","OXO","OOO"]source = [0,0]destination = [2,2]return = true

Move right twice and then down twice while avoiding the center obstacle.

Example 2

grid = ["OX","XO"]source = [0,0]destination = [1,1]return = false

Both first moves enter blocked cells.

Example 3

grid = ["OOO"]source = [0,2]destination = [0,0]return = false

Reaching the destination would require moving left.

Constraints

  • 1 <= grid.length, grid[i].length <= 500.
  • Every row has the same length, and the grid has at most 250000 cells.
  • Every cell is O or X.
  • source and destination each contain exactly two valid grid coordinates.

More Amazon problems

See Amazon hiring insights
Saved
public boolean hasRightDownPath(String[] grid, int[] source, int[] destination) {
  // write your code here
}
Checking account…