ProgrammingHardAmazon OA · Flipkart coding round

What is printed?

What is printed?
def can_jump(a):
    far = 0
    for i, x in enumerate(a):
        if i > far:
            return False
        far = max(far, i + x)
    return True
print(can_jump([2, 3, 1, 1, 4]), can_jump([3, 2, 1, 0, 4]))
  1. A. False True
  2. B. True True
  3. C. True False
  4. D. False False

Correct answer: C. True False

The greedy keeps the farthest reachable index and fails when a position lies beyond it. In [3, 2, 1, 0, 4] the reach stops at index 3, so index 4 cannot be reached.

More Python Programming questions

Exams that ask this topic