Quick Overview

Determine if robot path is bounded evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Determine if robot path is bounded

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

A robot starts at (0, 0) facing North on an infinite grid. It executes a string s consisting of 'G' (move forward 1 unit), 'L' (turn left 90°), and 'R' (turn right 90°) once, then repeats s forever. Return true if the robot stays within a bounded circle; otherwise return false. Justify your reasoning, discuss edge cases, and provide code.

Quick Answer: Determine if robot path is bounded evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

A robot starts at position (0, 0) on an infinite grid, initially facing North. It is given a string `instructions` consisting of three kinds of characters: - `'G'`: move forward 1 unit in the current direction. - `'L'`: turn 90 degrees to the left (counter-clockwise) without moving. - `'R'`: turn 90 degrees to the right (clockwise) without moving. The robot executes the entire string once, then repeats the same string forever. Return `true` if and only if there exists a circle of finite radius such that the robot never leaves it (i.e. its path is bounded); otherwise return `false`. The crucial observation: simulate exactly ONE pass over `instructions`. After one pass, the robot is at some offset `(x, y)` and facing some direction. The path is bounded if and only if (a) it ends back at the origin `(0, 0)`, OR (b) it does NOT end up facing North (its original direction). If it ends facing North but displaced from the origin, every cycle adds the same nonzero displacement and the robot drifts to infinity; if it ends facing any other direction, the cumulative rotation guarantees the net displacement cancels out within at most 4 cycles, keeping it within a bounded region.

Constraints

  • 1 <= instructions.length <= 100 (the empty string is also handled and is trivially bounded)
  • instructions consists only of the characters 'G', 'L', and 'R'
  • Coordinates fit comfortably in standard integer types within the constraint bounds

Examples

Input: ("GGLLGG",)

Expected Output: True

Explanation: Pass: G->(0,1), G->(0,2), L (now West), L (now South), G->(0,1), G->(0,0). Ends at origin, so bounded -> True.

Input: ("GG",)

Expected Output: False

Explanation: No turns: robot moves straight North forever, displacement (0,2) per cycle while still facing North. Unbounded -> False.

Hints

  1. You don't need to simulate forever — the behavior of all infinite repetitions is fully determined by ONE pass over the string. Track the final offset and final facing direction.
  2. Represent the four headings as direction vectors in clockwise order: North (0,1), East (1,0), South (0,-1), West (-1,0). A right turn is index +1 mod 4; a left turn is index +3 mod 4 (equivalently -1 mod 4).
  3. After one pass, the path is bounded iff the robot is back at the origin OR it is no longer facing North. If it faces North but is displaced, each cycle adds the same nonzero vector and it escapes to infinity. If it faces any other direction, the rotation makes the net 4-cycle displacement zero, so it stays bounded.

Loading coding console...