Bird
Raised Fist0

Identify the subtle bug that causes incorrect results on some inputs.

medium🐞 Bug Identification Q7 of Q15
Greedy Algorithms - Jump Game (Can Reach End?)
Consider the following code snippet for the Jump Game problem. Identify the subtle bug that causes incorrect results on some inputs. ```python def canJump(nums): maxReach = 0 for i, jump in enumerate(nums): if i >= maxReach: return False maxReach = max(maxReach, i + jump) return True ```
AThe condition should be 'if i > maxReach' instead of 'if i >= maxReach'
BmaxReach is not updated correctly with max()
CThe function should return False at the end instead of True
DThe loop should start from i=1 instead of i=0
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition 'if i >= maxReach'

    This causes immediate failure when i equals maxReach, but position i might still be reachable.
  2. Step 2: Correct condition is 'if i > maxReach'

    Only if current index is beyond maxReach should we return False; equality means reachable.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Off-by-one in condition causes premature failure [OK]
Quick Trick: Check strict inequality for reachability condition [OK]
Common Mistakes:
MISTAKES
  • Using >= instead of >
  • Misplacing return statements
Trap Explanation:
PITFALL
  • Using >= causes false negatives on reachable indices equal to maxReach.
Interviewer Note:
CONTEXT
  • Tests attention to boundary conditions in greedy implementation.
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