Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: In the binary array [1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1] at most 2 zeros may be flipped to 1. What is the length of the longest run of 1s that can be obtained?

  1. A. 7
  2. B. 8
  3. C. 11
  4. D. 3

Correct answer: B. 8

Zeros sit at indices 2, 3 and 7; the best window flips 3 and 7 and spans indices 3..10, length 8. Allowing only one flip gives 7, three flips gives 11, and 3 is the longest existing run without flipping.

More Coding Interview Problems questions

Exams that ask this topic