Katkat

Fix a Corrupted Bootloader Instruction

AnthropicSoftware Engineer · Technical Screen

Last updated

Simulate a virtual machine's instruction set, detect an infinite loop cycle, and find the single instruction modification (between jmp and nop) that allows the program to terminate normally.

Problem Statement

You are debugging a system bootloader that is stuck in an infinite execution loop. The bootloader executes a sequence of instructions, starting at index 0, with an internal accumulator register initialized to 0.

The bootloader supports three operations:

  • acc : Modifies the accumulator by +val or -val, then advances to the next instruction (ip + 1).
  • jmp : Jumps to the instruction at index ip + val relative to the current instruction pointer.
  • nop : Does nothing (no-operation) and advances to the next instruction (ip + 1).

The bootloader is supposed to terminate by attempting to execute the instruction immediately following the last instruction in the file (i.e., at index len(instructions)).

Due to corruption, exactly one instruction is wrong: either a single jmp should be a nop, or a single nop should be a jmp. No acc instruction is corrupted. Currently, the program enters an infinite loop by re-visiting an instruction it has already executed.

Write a function fix_bootloader(instructions: list[str]) -> int that determines which instruction was corrupted, fixes it, and returns the value of the accumulator immediately after the program successfully terminates.

Input Format

  • instructions: A list of strings, where each string is formatted as " " (e.g., "acc +5", "jmp -2", "nop +0").

Output Format

  • Return a single integer representing the final value of the accumulator.

Constraints

  • 1 <= len(instructions) <= 1000
  • -1000 len(instructions) is considered invalid/terminates prematurely.

Examples

Example 1

instructions = [
    "nop +0",
    "acc +1",
    "jmp +4",
    "acc +3",
    "jmp -3",
    "acc -99",
    "acc +1",
    "jmp -4",
    "acc +6"
]
# Output: 8

Explanation: If instruction 7 (jmp -4) is changed to nop -4, the execution order visits indices 0 -> 1 -> 2 -> 6 -> 7 -> 8 -> 9 (exit). The accumulator changes by +1 (index 1), +1 (index 6), and +6 (index 8), totaling 8.

Example 2

instructions = [
    "nop +2",
    "acc +5",
    "acc +10"
]
# Output: 15

Explanation: Currently nop +2 steps to index 1, visiting all instructions and exiting at index 3 with accumulator 15. Changing nop +2 to jmp +2 would jump straight to index 2, skipping acc +5, but index 0 was already a nop so mutating it is not needed here; in valid test cases where an infinite loop exists, exactly one instruction requires changing.

Hints

Hint 1

Write a helper function to simulate the program and return both whether it terminated cleanly (reached index N) and its final accumulator value.

Hint 2

Since the instruction list length is at most 1000, you can brute-force by iterating through every `jmp` or `nop` instruction, flipping it, and simulating execution.

Hint 3

To detect an infinite loop during simulation, maintain a `visited` set of instruction pointer indices; if you encounter an index already in `visited`, the program is looping.

Follow-up questions

  • How would you optimize your solution to run in O(N) time instead of O(N^2)?
  • How does your algorithm handle invalid jumps to negative indices or indices strictly greater than len(instructions)?
  • What changes would be required if multiple corrupted instructions were possible, and you needed to find the minimum number of edits to terminate?