DEV Community

Cover image for Amazon Coding Interview: Robot Return to Origin (LeetCode)
Matt Upham
Matt Upham

Posted on

Amazon Coding Interview: Robot Return to Origin (LeetCode)

Why should you practice coding problems?

Want a job at companies like Google, Facebook, Amazon, and more? Software engineering interviews mainly consist of data structures and algorithm problems.

You need to do a lot of these problems in order to succeed in these interviews. Below I’ll be going through an approach to a popular problem: Robot Return to Origin.

Please consider subscribing on YouTube by clicking here if you find this information useful!

Problem Statement & Approaach

The problem statement is:
“There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.”

This basically says a robot will be given a list of moves, and we need to determine if the robot ends at the same point where it started.

If you want a better sense visually of how this is done, check out my in-depth explanation on YouTube below.

LeetCode: Robot Return to Origin (Overview and Solution)

This problem might seem confusing at first, but take note that direction does not matter. We'll be dealing with an X and Y axis. A straightforward approach is to see if X and Y both equal 0 at the end (origin 0,0), since this is where the robot starts.

For Up (+1) and Down (-1), we can add these to a Y variable. For Left (-1) and Right (+1), we can add these to an X variable. Then we can loop through our string of moves, add the resulting move values to our X / Y variables, and compare our outcome to X == 0 and y == 0.


If you found this video useful, please consider subscribing to my YouTube Channel! I talk about software & technology, overviews of coding problems, and landing jobs in tech!

Also, make sure to check out our Discord community! 4700+ members, mentorship, discussion, and resources for learning to code!

On TikTok, I mainly do quick coding, tech and productivity tips. Check them out here: TikTok

Here are the social platforms where we can connect:

Thanks for reading, and feel free to DM any questions!

Top comments (3)

zarszz profile image
Ganjar Gingin Tahyudin


mattupham profile image
Matt Upham

Hey, anytime! Feel free to join our coding Discord too!

codinglanguages profile image
Your DevOps Guy

Follow up: How would you extend this to N dimensions?

Assuming you get the pairs of opposite directions (U-D, L-R, etc.)