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.
Conclusion
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:
YouTube
Discord
TikTok
Twitter
LinkedIn
Thanks for reading, and feel free to DM any questions!
Top comments (3)
thankss
Hey, anytime! Feel free to join our coding Discord too! link.mattupham.com/discord
Follow up: How would you extend this to N dimensions?
Assuming you get the pairs of opposite directions (U-D, L-R, etc.)