PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates the ability to simulate instruction-level program execution, reason about control flow and mutable program state (instruction pointer and accumulator), and diagnose a single corrupted control-flow operation.

  • medium
  • Anthropic
  • Coding & Algorithms
  • Software Engineer

Fix a Corrupted Bootloader Instruction

Company: Anthropic

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a bootloader program as a list of text instructions. Each instruction has an opcode and a signed integer value, such as `plus +1` or `jump -3`. The instruction pointer starts at index `0`, and the global accumulator starts at `0`. Instruction semantics: - `plus v`: add `v` to the accumulator, then move to the next instruction. - `next v`: do nothing with `v`, then move to the next instruction. - `jump v`: move to instruction index `current_index + v`. A program terminates normally when the instruction pointer moves to the position immediately after the last instruction. If the program attempts to execute an instruction that has already been executed during the same run, it is considered to be in an infinite loop. Exactly one instruction whose opcode is either `next` or `jump` was accidentally swapped: a `next` should have been a `jump`, or a `jump` should have been a `next`. `plus` instructions are always correct. Write a function that finds the single corrupted instruction, flips it, runs the corrected program, and returns the accumulator value after the corrected program terminates normally. You may assume there is exactly one valid fix. Example: ```text plus +1 next +0 plus +2 jump +2 plus +100 jump -3 plus +4 ``` In the original program, execution eventually repeats an instruction and loops. If instruction `5` is changed from `jump -3` to `next -3`, the program terminates normally, and the final accumulator is `7`.

Quick Answer: This question evaluates the ability to simulate instruction-level program execution, reason about control flow and mutable program state (instruction pointer and accumulator), and diagnose a single corrupted control-flow operation.

You are given a bootloader program as a list of text instructions. Each instruction is a string like `plus +1` or `jump -3`. The instruction pointer starts at index `0`, and the accumulator starts at `0`. Instruction semantics: - `plus v`: add `v` to the accumulator, then move to the next instruction. - `next v`: ignore `v`, then move to the next instruction. - `jump v`: move to instruction index `current_index + v`. A program terminates normally when the instruction pointer becomes exactly `n`, where `n` is the number of instructions. If an instruction is about to be executed for the second time during the same run, the program is in an infinite loop. Exactly one instruction whose opcode is either `next` or `jump` was accidentally swapped. `plus` instructions are always correct. Find the single corrupted instruction, flip it (`next` <-> `jump`), run the corrected program, and return the accumulator value after the corrected program terminates normally. You may assume there is exactly one valid fix.

Constraints

  • 1 <= n <= 200000
  • Each instruction is formatted as `<opcode> <signed_integer>`
  • The signed integer fits in a 32-bit signed range
  • Exactly one `next`/`jump` swap makes the program terminate normally
  • During the corrected execution, reaching index `n` means normal termination

Examples

Input: (["next +0", "plus +3", "next +2", "plus +4", "jump -3"],)

Expected Output: 7

Explanation: The program loops through indices 1 -> 2 -> 3 -> 4 -> 1. Flipping instruction 4 from jump -3 to next -3 makes the pointer move to index 5 and terminate. The accumulator is 3 + 4 = 7.

Input: (["jump +0"],)

Expected Output: 0

Explanation: This is a single-instruction loop. Flipping jump +0 to next +0 immediately moves the pointer to index 1, which is normal termination, with accumulator still 0.

Input: (["next +0", "plus +1", "jump +4", "plus +3", "jump -3", "plus -99", "plus +1", "jump -4", "plus +6"],)

Expected Output: 8

Explanation: The original program loops. Flipping instruction 7 from jump -4 to next -4 allows execution to continue to the last instruction and terminate. The final accumulator value is 8.

Input: (["plus +2", "next +4", "plus +5", "jump -1", "jump -2", "plus +1"],)

Expected Output: 3

Explanation: The original run loops between indices 2 and 3. Flipping instruction 1 from next +4 to jump +4 sends execution directly to index 5, where plus +1 is executed before termination. Accumulator: 2 + 1 = 3.

Input: (["plus +5", "jump +3", "plus -2", "next -2", "plus +4", "jump -4", "plus +1"],)

Expected Output: 10

Explanation: The original run follows 0 -> 1 -> 4 -> 5 -> 1 and loops. Flipping instruction 5 from jump -4 to next -4 moves execution to index 6, then the program terminates. Accumulator: 5 + 4 + 1 = 10.

Hints

  1. Think of each instruction as a node with one outgoing edge. Which instruction indices can already reach the end without any changes?
  2. Flipping an instruction that is never visited in the original run cannot affect the execution, so only the original path matters.
Last updated: Jun 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Persistent Memoization LRU Cache - Anthropic (hard)
  • Implement a Time-Aware Task Manager - Anthropic (hard)
  • Implement a Simplified DNS Resolver - Anthropic (hard)
  • Implement Task Management and Duplicate Detection - Anthropic (medium)
  • Implement a hierarchical file store - Anthropic (hard)