Ctrl K

Rover on Repeat

Medium
A rover stands at (0, 0) on an infinite plane, facing north (towards growing y). It is loaded with a program program made of commands: - G: drive 1 unit forward; - L: turn 90° left (counterclockwise); - R: turn 90° right (clockwise). The rover runs the program and, as soon as it reaches the end, starts it over again, forever. Return True if the rover's whole path fits inside some circle on the plane (it never wanders arbitrarily far), and False otherwise.
Example 1
program='GRG' → True

After one run the rover is at (1, 1) facing east. Each further run turns the path another 90°, so after four runs it is back at the start, walking around a square.

Example 2
program='GLRG' → False

The turns cancel out: the rover faces north again but has moved 2 cells, and every run takes it further away.

Example 3
program='GGRR' → True

The rover walks 2 cells and turns around; the second run brings it back to the start.

Constraints
  • 1 ≤ len(program) ≤ 100
  • program consists of the characters G, L, R
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMultiplying Digit Strings MediumMultiply Complex Numbers in Strings MediumTotal of a Fraction Chain MediumSolve for x MediumSum of Binary Strings Easy
loading Python…
Input
program='GRG'
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 143 tests on the server, hidden ones included.