Robot Return to Origin
Asked at Goldman Sachs
Problem
Robot Return to Origin gives you a string of moves (U, D, L, R) for a robot starting at (0, 0) and asks whether it ends back at the origin. It is a quick warm-up that checks you can simulate — or better, notice that only the counts matter.
Asked At
| Company | Difficulty | |
|---|---|---|
| Goldman Sachs | Easy | View all Goldman Sachs questions → |
How to Think About It
Simulate: keep x and y; U increments y, D decrements it, R increments x, L decrements it. Return whether both are 0 at the end.
Key observation: the order of moves never matters for the final position. The robot is home iff the number of Us equals the number of Ds and the number of Ls equals the number of Rs.
Quick exit: an odd-length move string can never return to the origin.
Walkthrough: "UD" -> y goes 1 then 0 -> true. "LL" -> x = -2 -> false.
Edge case: an empty string means the robot never moved — it is at the origin.
Optimal Approach
Step 1: x = y = 0.
Step 2: For each move, adjust x or y by ±1.
Step 3: Return x == 0 and y == 0.
Time: O(n). Space: O(1).
What Trips People Up in Real Interviews
Overthinking it with a set of visited positions. Only the final position matters.
Mixing up axes (for example, L changing y). Say the mapping out loud before coding.
Checking only the total count of moves, not per axis — "UUDDLR" returns home but "UULR" does not.
Not mentioning the counting shortcut. It shows you noticed that move order is irrelevant.
Solution Code
def judgeCircle(moves):
x = y = 0
for m in moves:
if m == 'U':
y += 1
elif m == 'D':
y -= 1
elif m == 'R':
x += 1
else:
x -= 1
return x == 0 and y == 0Frequently Asked Questions
What is the Robot Return to Origin problem?
Robot Return to Origin gives you a string of moves (`U`, `D`, `L`, `R`) for a robot starting at `(0, 0)` and asks whether it ends back at the origin. It is a quick warm-up that checks you can simulate — or better, notice that only the counts matter.
How do you solve Robot Return to Origin?
The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
What companies ask Robot Return to Origin?
Robot Return to Origin is asked at Goldman Sachs. It is a easy difficulty problem.
What are common mistakes on Robot Return to Origin?
- Overthinking it with a set of visited positions. Only the final position matters.
- Mixing up axes (for example, `L` changing `y`). Say the mapping out loud before coding.
- Checking only the total count of moves, not per axis — `"UUDDLR"` returns home but `"UULR"` does not.
- Not mentioning the counting shortcut. It shows you noticed that move order is irrelevant.