Bird
Raised Fist0

The following code attempts to solve the Jump Game problem. Identify the line that contains a subtle bug that causes incorrect results on some inputs.

medium🐞 Bug Identification Q14 of Q15
Greedy Algorithms - Jump Game (Can Reach End?)
The following code attempts to solve the Jump Game problem. Identify the line that contains a subtle bug that causes incorrect results on some inputs.
def canJump(nums):
    maxReach = 0
    for i, jump in enumerate(nums):
        # Bug: missing check if current index is beyond maxReach
        maxReach = max(maxReach, i + jump)
        if maxReach >= len(nums) - 1:
            return True
    return False
ALine 2: Initialization of maxReach
BLine 3: for loop header enumerating nums
CLine 4: Missing check if i > maxReach before updating maxReach
DLine 6: Checking if maxReach reaches or exceeds last index
Step-by-Step Solution
Solution:
  1. Step 1: Understand the missing condition

    The code does not check if the current index i is beyond maxReach, which means it may continue even when stuck.
  2. Step 2: Identify the bug line

    Line 4 updates maxReach without verifying if i is reachable, causing false positives on inputs with unreachable indices.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Adding "if i > maxReach: return False" fixes the bug [OK]
Quick Trick: Check if current index is reachable before updating maxReach [OK]
Common Mistakes:
MISTAKES
  • Forgetting to check i > maxReach
  • Assuming maxReach update alone suffices
Trap Explanation:
PITFALL
  • Without the check, code wrongly assumes progress is always possible, missing stuck cases.
Interviewer Note:
CONTEXT
  • Tests if candidate can spot subtle boundary condition bugs in greedy code.
Master "Jump Game (Can Reach End?)" in Greedy Algorithms

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Greedy Algorithms Quizzes