Skip to content
mlmentorship

Compress all reachable forward choices into one prefix boundary

Use maximum-jump prefix reachability, reject gaps before expansion, then change the destination or count minimum layers.

Published · 12 min read ·Core ·Intermediate

ML breadth · active recall

Practice before you read

8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.

ML breadth · closed-book attempt

Compress all reachable forward choices into one prefix boundary

Explain the mechanism, why it works, when it fails, and one alternative.

Problem

Input. A nonempty list nums of nonnegative integers, where nums[i] is the maximum jump length from position i. Output. Return True if some sequence of forward jumps reaches the final index; otherwise return False. Rules. Start at index zero. A jump may have any positive length up to the current position's value. The listed lengths are maxima, not mandatory exact distances. A zero cannot advance the position. Leave the input unchanged. A singleton returns True because its start is already the destination. Example. [2, 3, 1, 1, 4] returns True, using jumps from index 0 to 1 to 4. For [3, 2, 1, 0, 4], return False: no reachable position can pass index 3.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

30-second answer map

Scan only reachable positions and preserve the farthest index any processed jump can reach.

Problem trace

Separate reachable input: extend only the processed reachable prefix

Input and goalCheck whether jumps can reach the last array position.
Initialize the reachable frontierBefore scanning, farthest=0, so only index 0 is known reachable.
index=0farthest=03021120344
examplenums = [3, 2, 1, 0, 4]reachableRange0..0farthest0
index
0
farthest
0
example
nums = [3, 2, 1, 0, 4]

Recognize it
Each array value is a maximum forward jump and the question asks only whether the final position is reachable, not for a path or minimum jump count.
Keep true
Before index i is processed, every position through farthest is reachable using processed jumps; if i exceeds farthest, no earlier choice can reach i or anything beyond it.
Reuse it
When all feasible positions form a prefix, summarize every prior choice by its farthest boundary; this transfers to interval coverage, refueling reach, and minimum-jump layer scans.
Before scanning, farthest=0, so only index 0 is known reachable. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why can the value 4 at the last position not rescue this input?
Full interview answer 12 min read

Determine whether forward jumps can reach the final position from the start of an array.

Problem

Input. A nonempty list nums of nonnegative integers, where nums[i] is the maximum jump length from position i.

Output. Return True if some sequence of forward jumps reaches the final index; otherwise return False.

Rules. Start at index zero. A jump may have any positive length up to the current position’s value. The listed lengths are maxima, not mandatory exact distances. A zero cannot advance the position. Leave the input unchanged. A singleton returns True because its start is already the destination.

Example. [2, 3, 1, 1, 4] returns True, using jumps from index 0 to 1 to 4. For [3, 2, 1, 0, 4], return False: no reachable position can pass index 3.

Picture

The first value 3 permits landings 1, 2 and 3, filling the reachable prefix. Each later reachable position contributes another interval starting within that prefix. Their union stays contiguous, so the largest endpoint summarizes existence without preserving every jump choice.

After processing indices 0,1,2,3, farthest is always 3. Index 4 lies outside that boundary. Its own value 4 cannot invent a path to itself.

Problem trace

A large value cannot help if its index is unreachable

Worked example: an unreachable index cannot donate its large jumpFor (1,0,9), the reachable frontier stops at index 1. The large value at index 2 is never reachable and cannot extend that frontier.Only a reachable position may extendthe reachable frontierindex 01index 10index 29Index one is reachable but has jumpzero. Index two remains outside.The large nine is never read as avalid frontier contribution.
Read it this way: For (1,0,9), the reachable frontier stops at index 1. The large value at index 2 is never reachable and cannot extend that frontier. Can the value nine contribute a jump before its own position becomes reachable?
Reference drawing

Problem trace

A large value cannot help if its index is unreachable

Worked example: an unreachable index cannot donate its large jumpFor (1,0,9), the reachable frontier stops at index 1. The large value at index 2 is never reachable and cannot extend that frontier.Only a reachable position may extendthe reachable frontierindex 01index 10index 29Index one is reachable but has jumpzero. Index two remains outside.The large nine is never read as avalid frontier contribution.
Execution trace
Input and goalCheck whether jumps can reach the last array position.
Initialize the reachable frontierBefore scanning, farthest=0, so only index 0 is known reachable.
index=0farthest=03021120344
examplenums = [3, 2, 1, 0, 4]reachableRange0..0farthest0
index
0
farthest
0
example
nums = [3, 2, 1, 0, 4]

Recognize it
Each array value is a maximum forward jump and the question asks only whether the final position is reachable, not for a path or minimum jump count.
Keep true
Before index i is processed, every position through farthest is reachable using processed jumps; if i exceeds farthest, no earlier choice can reach i or anything beyond it.
Reuse it
When all feasible positions form a prefix, summarize every prior choice by its farthest boundary; this transfers to interval coverage, refueling reach, and minimum-jump layer scans.
Before scanning, farthest=0, so only index 0 is known reachable. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why can the value 4 at the last position not rescue this input?
Read it this way: For (1,0,9), the reachable frontier stops at index 1. The large value at index 2 is never reachable and cannot extend that frontier. Can the value nine contribute a jump before its own position becomes reachable?

Before an expansion, check index <= farthest. Only then may index+jump extend the boundary. The scalar stores reachability, not an explicit path or minimum jump count.

Code

def can_jump(nums: list[int]) -> bool:
   farthest = 0

   for index, jump in enumerate(nums):
      if index > farthest:
         return False
      farthest = max(farthest, index + jump)
   return True

Python’s enumerate retrieves a value before the check; an unreachable value is rejected before use in expansion. The function uses scalar work and scalar slots. Empty input returns True by its literal convention; it has no destination index. A single [0] also returns True.

Failure Check

With maximum jumps, [2,3,0,0,0] can use 0 -> 1 -> 4. With exact jumps, it is trapped at index 2. Exact distances, forbidden landings or backward moves require a new sufficiency argument.

Change the Destination

New task: Check a specified integer destination index.

Picture and change: Validate the target, then stop when the reachable boundary covers it. No earlier source can bridge a gap beyond that boundary.

def can_reach_target(nums, target):
   if not 0 <= target < len(nums):
      return False
   farthest = 0
   for index, jump in enumerate(nums):
      if index > farthest:
         return False
      farthest = max(farthest, index + jump)
      if farthest >= target:
         return True
   return False

Check: [3,2,1,0,4] reaches target 3 but not target 4. An invalid target returns False, including every target for empty input.

Use when: Only the destination changes.

Change to Minimum Jumps

New task: Return minimum hop count, or None when no destination is reachable.

Picture and change: Keep the current layer’s end and the next layer’s farthest reach. Increment the count only after scanning a whole equal-cost layer. Stalled growth means no next layer exists.

def minimum_jumps(nums):
   if not nums:
      return None
   jumps, layer_end, farthest = 0, 0, 0
   for index in range(len(nums) - 1):
      if index > farthest:
         return None
      farthest = max(farthest, index + nums[index])
      if index == layer_end:
         if farthest == layer_end:
            return None
         jumps += 1
         layer_end = farthest
   return jumps

Check: [2,3,1,1,4] returns 2; [3,2,1,0,4] returns None; [0] returns 0. Both adaptations retain linear scalar work and constant scalar slots under the maximum-jump contract. Layer boundaries add meaning absent from the Boolean reference.

Use when: Minimum equal-cost layers are required.

Transfer Check

Why does the farthest boundary not reconstruct a path?

Answer

It discards which predecessor first reached each destination. A witness needs predecessor information or another reconstruction method.