Magicsheet logo

Robot Return to Origin

Easy
23.3%
Updated 6/1/2025

Robot Return to Origin

What is this problem about?

The Robot Return to Origin interview question gives you a string of moves (U, D, L, R for up, down, left, right) that a robot makes on a 2D grid starting at the origin. Determine whether the robot returns to the origin (0, 0) after executing all moves. This is a straightforward coordinate tracking simulation problem.

Why is this asked in interviews?

Goldman Sachs, Yandex, Microsoft, Amazon, Google, and Bloomberg ask this problem as an entry-level simulation question. It validates basic coordinate tracking, understanding of opposing moves canceling out, and clean conditional branching. It also introduces the concept of "net displacement" — thinking about the aggregate effect of a sequence of moves rather than tracking each step.

Algorithmic pattern used

The pattern is coordinate counter or count-based cancellation. Track x and y displacements. For each character: R → x+1, L → x-1, U → y+1, D → y-1. After all moves, return x == 0 and y == 0. Alternatively, count each direction and check count('U') == count('D') and count('L') == count('R') — equivalent and more readable.

Example explanation

Moves: "UDRLRL"

  • U: y=1.
  • D: y=0.
  • R: x=1.
  • L: x=0.
  • R: x=1.
  • L: x=0.

Final: (0, 0) → return true.

Moves: "UURR":

  • U: y=1. U: y=2. R: x=1. R: x=2. Final: (2, 2) → return false.

Count-based: count('U')=2, count('D')=0 → 2 ≠ 0 → false. Immediately deterministic.

Common mistakes candidates make

  • Tracking position unnecessarily — the count-based approach is simpler and equally correct.
  • Confusing U/D with y axis and L/R with x axis — just be consistent.
  • Checking only one axis (e.g., only x) and missing that y must also be 0.
  • Not handling an empty string — vacuously returns true (robot never moved), which is correct.

Interview preparation tip

For the Robot Return to Origin coding problem, the string and simulation interview pattern reduces to a simple net-displacement check. The count-based approach (count('U') == count('D') and count('L') == count('R')) is the cleanest — it avoids a loop entirely in Python. Goldman Sachs interviewers often ask follow-ups like "what if the robot could also move diagonally?" — extend with NE, NW, SE, SW and decompose into x/y components.

Similar Questions