Calculate Score After Performing Instructions - Problem
Instruction Execution Simulator

Imagine you're implementing a simple virtual machine that processes a sequence of instructions! ๐Ÿ–ฅ๏ธ

You are given two arrays: instructions and values, both of size n. Your task is to simulate an instruction pointer moving through these arrays while maintaining a score.

The Rules:
โ€ข Start at index i = 0 with score = 0
โ€ข If instructions[i] == "add": Add values[i] to your score and move to i + 1
โ€ข If instructions[i] == "jump": Move to index i + values[i] (no score change)

The simulation ends when:
โ€ข You go out of bounds (i < 0 or i >= n), OR
โ€ข You attempt to revisit a previously executed instruction

Return the final score when the simulation terminates.

Input & Output

example_1.py โ€” Basic Simulation
$ Input: instructions = ["add", "jump", "add", "jump", "add"], values = [5, 2, 3, -2, 1]
โ€บ Output: 5
๐Ÿ’ก Note: Start at i=0: execute 'add' with value 5, score=5, i=1. At i=1: execute 'jump' with value 2, i=3. At i=3: execute 'jump' with value -2, i=1. At i=1: already visited, stop. Return score=5.
example_2.py โ€” Out of Bounds
$ Input: instructions = ["add", "add", "jump"], values = [1, 2, 10]
โ€บ Output: 3
๐Ÿ’ก Note: Start at i=0: execute 'add' with value 1, score=1, i=1. At i=1: execute 'add' with value 2, score=3, i=2. At i=2: execute 'jump' with value 10, i=12. i=12 is out of bounds, stop. Return score=3.
example_3.py โ€” Single Instruction
$ Input: instructions = ["jump"], values = [1]
โ€บ Output: 0
๐Ÿ’ก Note: Start at i=0: execute 'jump' with value 1, i=1. i=1 is out of bounds, stop. Return score=0 (no adds were executed).

Visualization

Tap to expand
Virtual Machine SimulationMemory (Instructions Array)addjumpaddjumpadd01234CPU StateInstruction Pointer: 0Score: 0Status: RunningExecute Current InstructionVisited Set (HashSet)013O(1) Lookup TimePrevents Infinite LoopsCurrent PositionCycle Detection Logicif (visited.contains(currentIndex)) { return score; // Cycle found!}
Understanding the Visualization
1
Initialize
Set up instruction pointer at 0, score at 0, and empty visited set
2
Check Bounds
Verify instruction pointer is within valid range
3
Detect Cycle
Check if current position was visited before using HashSet
4
Execute
Process the instruction: add to score or jump to new position
5
Update State
Mark current position as visited and update pointer
6
Repeat
Continue until out of bounds or cycle detected
Key Takeaway
๐ŸŽฏ Key Insight: Use a HashSet for O(1) cycle detection during instruction simulation - this prevents infinite loops while maintaining optimal performance!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each instruction is visited at most once, O(1) set operations

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

HashSet stores at most n visited indices

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • instructions[i] is either "add" or "jump"
  • -1000 โ‰ค values[i] โ‰ค 1000
  • The arrays instructions and values have the same length
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
23.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen