FastPrepObstacle-Aware Return to Origin

Obstacle-Aware Return to Origin

Waymo logoWaymo● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

A robot starts at (0,0). Commands U, D, L, and R move one grid cell. Return true only when the robot never enters any coordinate in obstacles and finishes back at (0,0). The origin is never blocked.

Function

returnsSafely(moves: String, obstacles: int[][]) → boolean

Examples

Example 1

moves = "URDL"obstacles = []return = true

The four moves form a safe loop.

Example 2

moves = "URDL"obstacles = [[-1,1]]return = false

The robot enters the blocked cell after U then R.

Example 3

moves = "UUDDR"obstacles = []return = false

The path is safe but finishes one column to the right of the origin.

Constraints

  • 0 <= moves.length <= 10^5 and every character is U, D, L, or R.
  • 0 <= obstacles.length <= 10^5; obstacle coordinates are unique and exclude the origin.

More Waymo problems

See Waymo hiring insights
public boolean returnsSafely(String moves, int[][] obstacles) {
    // Write your solution here.
}
moves"URDL"
obstacles[]
expectedtrue
Checking account…