FastPrepFind Two Non-Conflicting Maze Routes

Find Two Non-Conflicting Maze Routes

Google logoGoogle● HardNEW GRADPHONE SCREEN
Learn

Problem statement

A rectangular maze contains walls #, open cells ., starts a and b, and destinations A and B. The four labeled cells are distinct and are traversable.

Decide whether there is a four-direction path from a to A and a four-direction path from b to B such that the two paths share no cell. A path may not repeat a cell.

Return true when such a pair of routes exists.

Function

haveDisjointRoutes(maze: String[]) → boolean

Examples

Example 1

maze = ["...A",".#..","a..#","b..B"]return = true

The two agents can use disjoint corridors around the wall.

Example 2

maze = ["aBAb"]return = false

In the one-row source correction, any routes to the crossing destinations must share a cell.

Example 3

maze = ["a.A","###","b.B"]return = true

Each pair has its own isolated corridor.

Constraints

  • 1 <= rows, columns <= 6.
  • Every row has equal length and contains only #, ., a, A, b, or B.
  • Each of the four labels appears exactly once.

More Google problems

See Google hiring insights
public boolean haveDisjointRoutes(String[] maze) {
    // Write your code here.
}
maze["...A",".#..","a..#","b..B"]
expectedtrue
Checking account…