Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
There is a robot starting at the 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.
You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).
Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.
Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot move to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
Example 1:
Input: moves = "UD" Output: true Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
Example 2:
Input: moves = "LL" Output: false Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
Constraints:
1 <= moves.length <= 2 * 104moves only contains the characters 'U', 'D', 'L' and 'R'.Problem summary: There is a robot starting at the 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. You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down). Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise. Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot move to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"UD"
"LL"
number-of-provinces)execution-of-all-suffix-instructions-staying-in-a-grid)furthest-point-from-origin)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #657: Robot Return to Origin
class Solution {
public boolean judgeCircle(String moves) {
int x = 0, y = 0;
for (char c : moves.toCharArray()) {
switch (c) {
case 'U' -> y++;
case 'D' -> y--;
case 'L' -> x--;
case 'R' -> x++;
}
}
return x == 0 && y == 0;
}
}
// Accepted solution for LeetCode #657: Robot Return to Origin
func judgeCircle(moves string) bool {
x, y := 0, 0
for _, c := range moves {
switch c {
case 'U':
y++
case 'D':
y--
case 'L':
x--
case 'R':
x++
}
}
return x == 0 && y == 0
}
# Accepted solution for LeetCode #657: Robot Return to Origin
class Solution:
def judgeCircle(self, moves: str) -> bool:
x = y = 0
for c in moves:
match c:
case "U":
y += 1
case "D":
y -= 1
case "L":
x -= 1
case "R":
x += 1
return x == 0 and y == 0
// Accepted solution for LeetCode #657: Robot Return to Origin
struct Solution;
impl Solution {
fn judge_circle(moves: String) -> bool {
let mut x = 0;
let mut y = 0;
for c in moves.chars() {
match c {
'U' => y += 1,
'D' => y -= 1,
'L' => x -= 1,
'R' => x += 1,
_ => (),
}
}
x == 0 && y == 0
}
}
#[test]
fn test() {
assert_eq!(Solution::judge_circle("UD".to_string()), true);
assert_eq!(Solution::judge_circle("LL".to_string()), false);
}
// Accepted solution for LeetCode #657: Robot Return to Origin
function judgeCircle(moves: string): boolean {
let [x, y] = [0, 0];
for (const c of moves) {
if (c === 'U') {
y++;
} else if (c === 'D') {
y--;
} else if (c === 'L') {
x--;
} else {
x++;
}
}
return x === 0 && y === 0;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.