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 In Circle INPUT Instructions String: G G L L G G Starting Position: X Y R (0,0) North instructions = "GGLLGG" ALGORITHM STEPS 1 Initialize State pos=(0,0), dir=North(0) 2 Execute Instructions G: move, L/R: turn 90deg G: (0,0)-->(0,1) dir=N G: (0,1)-->(0,2) dir=N L: turn left, dir=W L: turn left, dir=S G: (0,2)-->(0,1) dir=S G: (0,1)-->(0,0) dir=S Final: (0,0), facing South After 2 cycles: back to start 3 Check Bounded pos==(0,0) OR dir!=North 4 Return Result (0,0)==(0,0) --> true BOUNDED! FINAL RESULT Robot Path (2 cycles): (0,0) Cycle 1: (0,0)-->(0,0) Facing: N --> S Cycle 2: (0,0)-->(0,0) Facing: S --> N Output: true Robot is bounded [OK] Verified Key Insight: The robot is bounded in a circle if after one sequence: (1) it returns to origin, OR (2) it's not facing North. If not facing North, after at most 4 cycles it will return to origin. Direction change guarantees a closed loop. Time: O(n) | Space: O(1) where n = length of instructions TutorialsPoint - Robot Bounded In Circle | Optimal Solution
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