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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two arrays, instructions and values, both of size n.
You need to simulate a process based on the following rules:
i = 0 with an initial score of 0.instructions[i] is "add":
values[i] to your score.(i + 1).instructions[i] is "jump":
(i + values[i]) without modifying your score.The process ends when you either:
i < 0 or i >= n), orReturn your score at the end of the process.
Example 1:
Input: instructions = ["jump","add","add","jump","add","jump"], values = [2,1,3,1,-2,-3]
Output: 1
Explanation:
Simulate the process starting at instruction 0:
"jump", move to index 0 + 2 = 2."add", add values[2] = 3 to your score and move to index 3. Your score becomes 3."jump", move to index 3 + 1 = 4."add", add values[4] = -2 to your score and move to index 5. Your score becomes 1."jump", move to index 5 + (-3) = 2.Example 2:
Input: instructions = ["jump","add","add"], values = [3,1,1]
Output: 0
Explanation:
Simulate the process starting at instruction 0:
"jump", move to index 0 + 3 = 3.Example 3:
Input: instructions = ["jump"], values = [0]
Output: 0
Explanation:
Simulate the process starting at instruction 0:
"jump", move to index 0 + 0 = 0.Constraints:
n == instructions.length == values.length1 <= n <= 105instructions[i] is either "add" or "jump".-105 <= values[i] <= 105Problem summary: You are given two arrays, instructions and values, both of size n. You need to simulate a process based on the following rules: You start at the first instruction at index i = 0 with an initial score of 0. If instructions[i] is "add": Add values[i] to your score. Move to the next instruction (i + 1). If instructions[i] is "jump": Move to the instruction at index (i + values[i]) without modifying your score. The process ends when you either: Go out of bounds (i.e., i < 0 or i >= n), or Attempt to revisit an instruction that has been previously executed. The revisited instruction is not executed. Return your score at the end of the process.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["jump","add","add","jump","add","jump"] [2,1,3,1,-2,-3]
["jump","add","add"] [3,1,1]
["jump"] [0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3522: Calculate Score After Performing Instructions
class Solution {
public long calculateScore(String[] instructions, int[] values) {
int n = values.length;
boolean[] vis = new boolean[n];
long ans = 0;
int i = 0;
while (i >= 0 && i < n && !vis[i]) {
vis[i] = true;
if (instructions[i].charAt(0) == 'a') {
ans += values[i];
i += 1;
} else {
i = i + values[i];
}
}
return ans;
}
}
// Accepted solution for LeetCode #3522: Calculate Score After Performing Instructions
func calculateScore(instructions []string, values []int) (ans int64) {
n := len(values)
vis := make([]bool, n)
i := 0
for i >= 0 && i < n && !vis[i] {
vis[i] = true
if instructions[i][0] == 'a' {
ans += int64(values[i])
i += 1
} else {
i += values[i]
}
}
return
}
# Accepted solution for LeetCode #3522: Calculate Score After Performing Instructions
class Solution:
def calculateScore(self, instructions: List[str], values: List[int]) -> int:
n = len(values)
vis = [False] * n
ans = i = 0
while 0 <= i < n and not vis[i]:
vis[i] = True
if instructions[i][0] == "a":
ans += values[i]
i += 1
else:
i = i + values[i]
return ans
// Accepted solution for LeetCode #3522: Calculate Score After Performing Instructions
// 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 #3522: Calculate Score After Performing Instructions
// class Solution {
// public long calculateScore(String[] instructions, int[] values) {
// int n = values.length;
// boolean[] vis = new boolean[n];
// long ans = 0;
// int i = 0;
//
// while (i >= 0 && i < n && !vis[i]) {
// vis[i] = true;
// if (instructions[i].charAt(0) == 'a') {
// ans += values[i];
// i += 1;
// } else {
// i = i + values[i];
// }
// }
//
// return ans;
// }
// }
// Accepted solution for LeetCode #3522: Calculate Score After Performing Instructions
function calculateScore(instructions: string[], values: number[]): number {
const n = values.length;
const vis: boolean[] = Array(n).fill(false);
let ans = 0;
let i = 0;
while (i >= 0 && i < n && !vis[i]) {
vis[i] = true;
if (instructions[i][0] === 'a') {
ans += values[i];
i += 1;
} else {
i += values[i];
}
}
return ans;
}
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.