Robot Bounded In Circle

A robot starts at (0, 0) on an infinite plane facing north. It understands three instructions: "G" moves one unit forward, "L" turns 90 degrees left and "R" turns 90 degrees right. The robot repeats the given instructions forever. Return true if there is a circle on the plane that the robot never leaves.

30:00

Robot Bounded In Circle
medium
Free written solution
Topics
Companies

A robot starts at (0, 0) on an infinite plane facing north. It understands three instructions: "G" moves one unit forward, "L" turns 90 degrees left and "R" turns 90 degrees right. The robot repeats the given instructions forever. Return true if there is a circle on the plane that the robot never leaves.

Example 1:
Input: {"instructions":"GGLLGG"}
Output: true
Constraints:
  • 1≤1 \leq instructions.length ≤100\leq 100

  • instructions[i] is 'G', 'L' or 'R'.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"instructions":"GGLLGG"}

The robot starts at (0, 0) facing north. Run the 6 instructions once and see where it ends up and which way it faces.

instructions · run 1

G
G
L
L
G
G

the robot's path

━ run 1

○ origin

position = (0, 0)
facing = ↑ north

after one run

run the instructions once…
Variables
VariableValue
x0
y0
dnorth
DepthFunction Call
Stack empty
0/8