FastPrepUniversal Exit Command Sequence

Universal Exit Command Sequence

Google logoGoogle● HardFULLTIMEONSITE INTERVIEW

Problem statement

A rectangular maze uses # for walls, . for passable cells that may be the unknown start, and exactly one E exit. You must choose one fixed command string over U, D, L, and R.

Every possible start executes every command without feedback. A move into a wall or outside the grid leaves that possibility in place. A possibility that enters E terminates and is removed. Return the shortest command string that makes every possible start reach the exit. Break ties by exploring commands in U,D,L,R order.

Function

universalExitCommands(grid: String[]) → String

Examples

Example 1

grid = ["..E"]return = "RR"

Two right moves merge and then exit both possible starts.

Example 2

grid = ["..","E#"]return = "LD"

A horizontal merge followed by a downward move reaches the exit.

Constraints

  • 1 <= rows, columns <= 20.
  • The grid is rectangular and contains exactly one E.
  • There are at most 20 dot cells.
  • A universal sequence is guaranteed to exist.

More Google problems

See Google hiring insights
Saved
public String universalExitCommands(String[] grid) {
  // write your code here
}
Checking account…