Magicsheet logo

Robot Bounded In Circle

Medium
70.4%
Updated 6/1/2025

Robot Bounded In Circle

What is this problem about?

The Robot Bounded In Circle interview question gives you a sequence of instructions (G = go forward, L = turn left, R = turn right) that a robot executes repeatedly on an infinite plane starting at the origin facing north. Determine whether the robot is bounded in a circle — meaning it will always return to the origin eventually — or will drift off to infinity.

Why is this asked in interviews?

This problem is asked at Apple, Goldman Sachs, Microsoft, Nvidia, Airbnb, Amazon, LinkedIn, Google, and Bloomberg because it requires mathematical reasoning about periodic motion and direction cycles. It tests whether candidates can derive a clean geometric invariant rather than simulating infinitely many steps. It appears in robotics, simulation, and path planning systems.

Algorithmic pattern used

The pattern is mathematical simulation with cycle analysis. Simulate one cycle of instructions, tracking the robot's net displacement and final direction. After one cycle, the robot is bounded if either: (1) it is back at the origin (net displacement is zero), OR (2) it is not facing north (facing south, east, or west). In cases 2, executing at most 4 cycles will return it to the origin due to rotational symmetry.

Only simulate one pass — no need to simulate multiple cycles.

Example explanation

Instructions: "GLRG"

Start: position (0,0), facing North.

  • G: move north → (0,1).
  • L: face West.
  • R: face North.
  • G: move north → (0,2).

After one cycle: position (0,2), facing North. Not at origin and facing North → NOT bounded (will drift north forever). Return false.

Instructions: "GL"

  • G: (0,1). L: face West. After one cycle: position (0,1), facing West. Not at origin but NOT facing North → bounded. Return true.

(After 4 cycles it returns to origin.)

Common mistakes candidates make

  • Simulating many cycles instead of deriving the one-cycle invariant.
  • Using incorrect direction tracking — maintain a direction index (0=N, 1=E, 2=S, 3=W) and update with (direction + 1) % 4 for R and (direction - 1 + 4) % 4 for L.
  • Forgetting that "not at origin but facing non-north" is also bounded — only "at origin OR not facing north" covers all bounded cases.
  • Using floating-point coordinates when integer arithmetic suffices.

Interview preparation tip

For the Robot Bounded In Circle coding problem, the math and simulation interview pattern requires understanding the geometric invariant. Memorize the key insight: simulate one cycle, check if position is (0,0) or direction is not North. Bloomberg and Airbnb interviewers often ask "why does non-north direction guarantee boundedness?" — be ready to explain the 4-cycle rotational symmetry argument clearly.

Similar Questions