Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA

Jump game: array a = [2, 3, 1, 1, 4, 2, 1, 3, 1, 2]. Starting at index 0, from index i you may jump forward to any index between i+1 and i+a[i] (inclusive). What is the minimum number of jumps needed to reach the last index?

  1. A. 4
  2. B. 5
  3. C. 3
  4. D. 6

Correct answer: A. 4

Greedy BFS by layers: the set of positions reachable in k jumps is a contiguous range, and extending it by the furthest reach gives the minimum. Taking the first reachable index each time instead of the furthest one costs an extra jump.

More Coding Interview Problems questions

Exams that ask this topic