Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Infinite Loop Instruction

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Find Infinite Loop Instruction. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Anthropic Claude Code uses a small deterministic instruction stream during an internal execution check. Given the instructions and initial register values, determine whether execution terminates or enters an infinite loop. If it loops, return the index of the first instruction in the repeating cycle. Otherwise, return -1.

A machine state consists of the program counter and the values of registers a, b, and c. Tracking only the program counter is incorrect because the same instruction can be reached with different register values.

Formal Specification

Implement find_infinite_loop(instructions, initial). instructions is a list of instruction arrays. initial is a dictionary containing integer values for a, b, and c. Register values use unsigned 8-bit arithmetic, so every set and add result is reduced modulo 256.

Supported instructions are ['set', register, value], ['add', register, value], ['jmp', offset], ['jnz', register, offset], and ['halt']. The program counter starts at 0. A jump offset is relative to the current instruction. Reaching halt or moving outside the instruction list terminates execution.

Constraints

  • 1 <= len(instructions) <= 200,000
  • Register values are always normalized modulo 256
  • At most 1,000,000 distinct machine states are visited
  • Offsets and literal values fit within signed 32-bit integers
  • Instructions are valid and use only registers a, b, and c

Function Signature

def find_infinite_loop(instructions, initial):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output