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.
You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0.
In the ith move, you can choose one of the following directions:
moves[i] = 'L' or moves[i] = '_'moves[i] = 'R' or moves[i] = '_'Return the distance from the origin of the furthest point you can get to after n moves.
Example 1:
Input: moves = "L_RL__R" Output: 3 Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR".
Example 2:
Input: moves = "_R__LL_" Output: 5 Explanation: The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL".
Example 3:
Input: moves = "_______" Output: 7 Explanation: The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR".
Constraints:
1 <= moves.length == n <= 50moves consists only of characters 'L', 'R' and '_'.Problem summary: You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0. In the ith move, you can choose one of the following directions: move to the left if moves[i] = 'L' or moves[i] = '_' move to the right if moves[i] = 'R' or moves[i] = '_' Return the distance from the origin of the furthest point you can get to after n moves.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"L_RL__R"
"_R__LL_"
"_______"
robot-return-to-origin)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2833: Furthest Point From Origin
class Solution {
public int furthestDistanceFromOrigin(String moves) {
return Math.abs(count(moves, 'L') - count(moves, 'R')) + count(moves, '_');
}
private int count(String s, char c) {
int cnt = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == c) {
++cnt;
}
}
return cnt;
}
}
// Accepted solution for LeetCode #2833: Furthest Point From Origin
func furthestDistanceFromOrigin(moves string) int {
count := func(c string) int { return strings.Count(moves, c) }
return abs(count("L")-count("R")) + count("_")
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2833: Furthest Point From Origin
class Solution:
def furthestDistanceFromOrigin(self, moves: str) -> int:
return abs(moves.count("L") - moves.count("R")) + moves.count("_")
// Accepted solution for LeetCode #2833: Furthest Point From Origin
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2833: Furthest Point From Origin
// class Solution {
// public int furthestDistanceFromOrigin(String moves) {
// return Math.abs(count(moves, 'L') - count(moves, 'R')) + count(moves, '_');
// }
//
// private int count(String s, char c) {
// int cnt = 0;
// for (int i = 0; i < s.length(); ++i) {
// if (s.charAt(i) == c) {
// ++cnt;
// }
// }
// return cnt;
// }
// }
// Accepted solution for LeetCode #2833: Furthest Point From Origin
function furthestDistanceFromOrigin(moves: string): number {
const count = (c: string) => moves.split('').filter(x => x === c).length;
return Math.abs(count('L') - count('R')) + count('_');
}
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.