Robot Bounded In Circle - Problem

Imagine a robot placed on an infinite 2D plane at the origin (0, 0), initially facing north. The robot receives a sequence of instructions and will repeat them infinitely.

The robot understands three commands:

  • 'G' - Move forward 1 unit in the current direction
  • 'L' - Turn 90 degrees left (counterclockwise)
  • 'R' - Turn 90 degrees right (clockwise)

The Challenge: Determine if the robot will stay within a bounded circular area forever, or if it will eventually escape to infinity.

Key Insight: If after executing the instruction sequence once, the robot either returns to the origin OR is not facing north, then it will eventually form a cycle and stay bounded!

Example: Instructions "GGLLGG" will make the robot walk in a square pattern, never escaping.

Input & Output

example_1.py โ€” Basic Square Pattern
$ Input: instructions = "GGLLGG"
โ€บ Output: true
๐Ÿ’ก Note: Robot moves North 2 units, turns left twice (now facing South), moves South 2 units. After one cycle: back at origin (0,0) but facing South. Since it returns to origin, it's bounded in a cycle.
example_2.py โ€” Straight Line Escape
$ Input: instructions = "GG"
โ€บ Output: false
๐Ÿ’ก Note: Robot moves North 2 units. After one cycle: at position (0,2) and still facing North. Each cycle will move it further North by 2 units, so it escapes to infinity.
example_3.py โ€” Circular Pattern
$ Input: instructions = "GL"
โ€บ Output: true
๐Ÿ’ก Note: Robot moves North 1 unit, then turns left (facing West). After one cycle: at (0,1) facing West. Since direction changed, after 4 cycles it will return to origin, forming a square pattern.

Visualization

Tap to expand
OriginGLGRDecision MatrixAt Origin? โ†’ BOUNDEDNot North? โ†’ BOUNDEDDisplaced + North โ†’ ESCAPESMathematical GuaranteeBoundedRobot stays withincircular boundary
Understanding the Visualization
1
Initial Setup
Robot starts at origin (0,0) facing North
2
Execute Instructions
Process each instruction: G moves forward, L/R turn 90 degrees
3
Analyze Final State
Check position and direction after one complete cycle
4
Apply Mathematical Rule
Bounded if: at origin OR not facing North
Key Takeaway
๐ŸŽฏ Key Insight: The robot can only escape to infinity if it's displaced from origin while still facing the original direction (North). Any change in direction will eventually create a cycle due to the 4-fold rotational symmetry of the plane.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the instruction string of length n

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing position coordinates and direction, constant extra space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค instructions.length โ‰ค 100
  • instructions[i] is 'G', 'L' or 'R'
  • The robot will repeat the instructions infinitely
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 22
68.9K Views
Medium-High Frequency
~15 min Avg. Time
1.8K 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