Robot Bounded In Circle (LeetCode 1041): Solution and Explanation

Medium
Math
String
Simulation

A robot starts at the origin facing north and repeats a string of G, L and R instructions forever. G moves one step forward, L and R turn 90 degrees. Return true if the robot stays inside some circle forever, meaning its path is bounded.

Intuition

The robot repeats the instructions forever, so you cannot simulate to the end. The trick is to describe one full run by what it does to the robot: it moves the robot by some offset (dx, dy) and turns it by some multiple of 90 degrees.

If the robot is back at the origin after one run, the second run is an exact copy of the first, and so is every run after it. The path is a closed loop, so it is bounded.

If the robot ends away from the origin but faces a new direction, the next run traces the same shape rotated by that turn. After a 180 degree turn, run two cancels run one and the robot is home after two runs. After a 90 degree turn either way, four rotated copies of the offset add up to zero, so the robot is home after four runs. Either way the motion is a closed loop.

The only way to escape is to end the run away from the origin while still facing north. Then every run adds the same offset in the same direction and the robot marches off to infinity. So a single simulated run decides the answer.

Approach: Simulate one run

  1. Store the four unit moves in clockwise order: north (0, 1), east (1, 0), south (0, -1), west (-1, 0).
  2. Start with x = y = 0 and direction index d = 0 (north).
  3. For G, add dirs[d] to the position. For L, set d = (d + 3) % 4. For R, set d = (d + 1) % 4.
  4. After the loop, return true if (x, y) == (0, 0) or d != 0.

Step-by-step example

instructions = "GGLLGG"
  1. Start at (0, 0) facing north.
  2. G, G: the robot walks to (0, 1) and then (0, 2).
  3. L, L: it turns left twice and now faces south.
  4. G, G: it walks back to (0, 1) and then (0, 0).
  5. After one run it is at the origin, so the path is a closed loop. Return true.

Code: Python, Java, C++

1class Solution:
2    def isRobotBounded(self, instructions: str) -> bool:
3        dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
4        x = y = d = 0
5        for c in instructions:
6            if c == 'G':
7                x += dirs[d][0]
8                y += dirs[d][1]
9            elif c == 'L':
10                d = (d + 3) % 4
11            else:
12                d = (d + 1) % 4
13        return (x == 0 and y == 0) or d != 0
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass over the instruction string, with constant work per character.

Space

O(1)

Only the position, the direction index and a fixed table of four moves are stored.

Other approaches

Simulate four runs (time O(n), space O(1))

Run the instructions four times and check whether the robot is back at the origin. Four runs always bring a turning robot home, and a robot that does not turn is home only if its offset is zero, so this gives the same answer with four times the work.

Complex number rotation (time O(n), space O(1))

Represent the position and direction as complex numbers, multiplying the direction by i for a left turn and by -i for a right turn. It is the same simulation with tidier turn arithmetic.

Common mistakes

  • Checking only whether the robot returns to the origin after one run. "GL" ends at (0, 1) facing west but still loops after four runs.
  • Checking only the direction. "GLGLGLGL" ends facing north, yet it is back at the origin, so it is bounded.
  • Turning the wrong way. With directions in clockwise order, a right turn adds 1 and a left turn adds 3, not subtracts 1, so the index stays non-negative.
  • Trying to simulate until the robot repeats a state with a large step limit. It works but hides the simple argument and wastes time.

Frequently asked questions

Why does a robot that ends facing another direction always come back?

Each run moves it by the previous run's offset rotated by the same angle. Rotating by 180 degrees twice, or by 90 degrees four times, gives offsets that cancel out, so after two or four runs the robot is at the origin facing north and the whole cycle repeats.

Why is facing north with a nonzero offset unbounded?

Every run starts in the same orientation, so it adds exactly the same offset. After k runs the robot is k times that offset away from the origin, which grows without limit.

Does the robot need to visit the origin during a run to be bounded?

No. It only matters where it is and which way it faces at the end of a run. The path within a run is finite, and the runs repeat in a cycle of at most four.

What is the time complexity of Robot Bounded In Circle?

O(n) time for one pass over the instructions and O(1) extra space.