Robot Bounded In Circle - Problem

On an infinite plane, a robot initially stands at (0, 0) and faces north.

Note that:

  • The north direction is the positive direction of the y-axis.
  • The south direction is the negative direction of the y-axis.
  • The east direction is the positive direction of the x-axis.
  • The west direction is the negative direction of the x-axis.

The robot can receive one of three instructions:

  • "G": go straight 1 unit.
  • "L": turn 90 degrees to the left (i.e., anti-clockwise direction).
  • "R": turn 90 degrees to the right (i.e., clockwise direction).

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Input & Output

Example 1 — Returns to Origin
$ Input: instructions = "GGLLGG"
Output: true
💡 Note: After executing once: robot moves north 2 units, turns left twice (now facing south), moves south 2 units, ending at origin (0,0). Since it's back at the starting position, it will repeat the same path forever.
Example 2 — Different Direction
$ Input: instructions = "GG"
Output: false
💡 Note: After executing once: robot moves north 2 units to position (0,2) and still faces north. It will keep moving north indefinitely, so it's unbounded.
Example 3 — Turns Only
$ Input: instructions = "GL"
Output: true
💡 Note: After executing once: robot moves north 1 unit to (0,1) then turns left to face west. Since it's not facing north anymore, it will eventually return to origin after 4 cycles.

Constraints

  • 1 ≤ instructions.length ≤ 100
  • instructions[i] is 'G', 'L' or 'R'

Visualization

Tap to expand
Robot Bounded Problem OverviewInput: "GGLLGG"Simulate RobotMovementOutput: true(0,0) Start(0,0) EndRobot returns to origin → Bounded!
Understanding the Visualization
1
Input
Instruction string like "GGLLGG"
2
Simulate
Execute instructions once, track position and direction
3
Check
Bounded if at origin OR not facing north
Key Takeaway
🎯 Key Insight: Check position and direction after one cycle - if at origin OR not facing north, robot is bounded
Asked in
Google 25 Amazon 18 Microsoft 15 Apple 12
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen