EasyStringSimulationUpdated Sep 2026

657. Robot Return to Origin

Asked at Goldman Sachs

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

String

Companies

1

Problem

A robot starts at the origin (0, 0) on a flat 2D plane. You are given a string moves, where each character is one step the robot takes: 'U' moves it up by 1, 'D' moves it down by 1, 'R' moves it right by 1, and 'L' moves it left by 1.

The robot always moves the same distance per step, and which way it is "facing" does not matter — 'R' always means one step right, and so on. The moves are applied in order, from the first character to the last.

Return true if the robot is back at (0, 0) after all the moves are done, and false otherwise.

Track the robot on the plane

planex=-1x=0x=1y=1y=0y=-1(0,1)(-1,0)(0,0)(1,0)(0,-1)Start at the origin (0, 0), marked greenUD01"UD": up then down — the two moves cancel out
Each move changes one coordinate by 1; we ask whether the final point is (0, 0).

Constraints

  • ●1 <= moves.length <= 2 * 10^4
  • ●moves only contains the characters 'U', 'D', 'L' and 'R'

Examples

Example 1
Inputmoves = "UD"
Outputtrue
WhyThe robot goes up one step to (0, 1), then down one step back to (0, 0). It ends where it started.
Example 2
Inputmoves = "LL"
Outputfalse
WhyTwo left steps put the robot at (-2, 0). That is not the origin, so the answer is false.

Asked At

1 companies
CompanyDifficulty
Goldman SachsEasyView all Goldman Sachs questions →

How to Think About It

1

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.

2

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.

3

Quick exit: an odd-length move string can never return to the origin.

4

Walkthrough: "UD" -> y goes 1 then 0 -> true. "LL" -> x = -2 -> false.

5

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).

One counter per axis

x = 0, y = 0'U' → y = 1'D' → y = 0Walking through "UD": y rises once and falls onceU: y += 1 D: y -= 1R: x += 1 L: x -= 1return x == 0 and y == 0
U and D change y, L and R change x. The robot is home only when both counters are 0.
ComplexityTime O(n)Space O(1)We read each move once and only keep two counters, so the cost grows with the length of the string and the extra memory stays constant.

What Trips People Up in Real Interviews

1

Overthinking it with a set of visited positions. Only the final position matters.

2

Mixing up axes (for example, L changing y). Say the mapping out loud before coding.

3

Checking only the total count of moves, not per axis — "UUDDLR" returns home but "UULR" does not.

4

Not mentioning the counting shortcut. It shows you noticed that move order is irrelevant.

Solution Code

O(n) time
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 == 0

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently 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 runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

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.