Problem · Array

Repair the Bootloader Program

Learn this problem
MediumAnthropicFULLTIMEONSITE INTERVIEW

Problem statement

Interview report note: The reports described a bootloader program with plus, next, and jump instructions, plus one swapped next/jump instruction that causes a loop. This practice version is an estimated 90% match for the repair task; the original numeric constraints and the separate loop-reporting output were not shared.

A bootloader executes a list of instructions using a program counter pc and an accumulator that starts at 0. Each instruction contains an operation and a signed integer operand.

Instruction semantics

  • plus x: add x to the accumulator, then advance to the next instruction.
  • next x: ignore x and advance to the next instruction.
  • jump x: move the program counter by x.

The program terminates normally when the program counter moves past the final instruction. It is looping if it is about to execute an instruction that it has already executed.

Repair rule

Exactly one instruction was corrupted: a next operation was changed to jump, or a jump operation was changed to next. Its operand is still correct. Changing that one operation back makes the program terminate normally.

Repair the corrupted instruction and return the accumulator value after the repaired program terminates.

Function

repairBootloader(instructions: String[]) → int

Examples

Example 1

instructions = ["plus +1","next +0","jump -1","plus +3"]return = 4

The original program repeats instructions 1 and 2. Changing instruction 2 from jump -1 to next -1 lets execution reach the final plus +3 instruction and terminate with accumulator 1 + 3 = 4.

More Anthropic problems

drafts saved locally
public int repairBootloader(String[] instructions) {
  // write your code here
}
instructions["plus +1","next +0","jump -1","plus +3"]
expected4
checking account