KatInterview

Repair a Looping Script by Swapping One Instruction

Arrays & HashingAuto-graded · 20 test cases
MediumAuto-gradedArrays & Hashingsimulationcycle-detectionbrute-force

Last updated

Tests simulation, cycle detection, and brute-force search by finding the single jmp/nop swap that lets a small virtual machine terminate cleanly.

Problem

You are given a small script that runs on a toy virtual machine. The machine has a single integer register called the accumulator, initialized to 0, and an instruction pointer (ip) that starts at index 0.

The script supports exactly three instruction types:

  • acc N — adds N to the accumulator, then moves to the next instruction (ip += 1).
  • jmp N — moves the instruction pointer by N relative to its current position (ip += N).
  • nop N — does nothing with N and simply advances to the next instruction (ip += 1).

The script is considered to have terminated successfully when the instruction pointer lands exactly one past the last instruction (i.e., ip == len(script)). Any other out-of-range value of ip is an invalid crash, not a successful termination.

Unfortunately, the script contains exactly one corrupted instruction: either a single jmp that should be a nop, or a single nop that should be a jmp. No acc instruction is ever corrupted. As a result, the script may revisit an instruction it has already executed, putting the machine into an infinite loop.

Write a function repair_script(instructions) that identifies the corrupted instruction, corrects it, and returns the value of the accumulator the moment the repaired script terminates successfully.

Note: if the script already terminates without any modification, return the accumulator value from that unmodified run.

Input

  • instructions: a list of strings, each formatted as "<op> <N>" where <op> is acc, jmp, or nop, and <N> is a signed integer (e.g., "acc +5", "jmp -2", "nop +0").

Output

A single integer: the accumulator value after the (possibly repaired) script terminates.

Constraints

  • 1 <= len(instructions) <= 1000
  • Each argument N satisfies -1000 <= N <= 1000.
  • Exactly one jmp or nop instruction is corrupted (or the script already terminates cleanly).
  • An out-of-range ip (negative, or greater than len(instructions)) is a crash and does not count as successful termination.
  • Successful termination requires ip == len(instructions) exactly.

Examples

Example 1

Input: repair_script(['nop +0', 'acc +1', 'jmp +4', 'acc +3', 'jmp -3', 'acc -99', 'acc +1', 'jmp -4', 'acc +6'])

Output: 8

Example 2

Input: repair_script(['nop +2', 'acc +5', 'acc +10'])

Output: 15

Hints

Hint 1

Write a helper that simulates one run of the script and reports whether it terminated cleanly or hit a repeated instruction. A set of visited indices is all you need for cycle detection — the machine is deterministic, so revisiting any index means an infinite loop.

Hint 2

Since at most 1000 instructions can be swapped, you can afford to try each jmp/nop flip one at a time and re-run the simulator for each candidate. That is O(N²) total, which is well within budget for N ≤ 1000.

Hint 3

Check the unmodified script first. If it already terminates, return that result immediately. Otherwise, try flips in index order and return the accumulator from the first run that terminates cleanly.