Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

In the binary array [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0] you may flip at most 2 zeros to ones. What is the length of the longest run of consecutive ones achievable? (Sliding window with at most 2 zeros inside.)

  1. A. 10
  2. B. 9
  3. C. 12
  4. D. 8

Correct answer: C. 12

Keep a window with at most 2 zeros; when a third zero enters, move the left edge past the earliest zero. Window [0..11] (eight 1s, the 0 0, then 1 1) has length 12. Adding 2 to the longest run (8 + 2 = 10) ignores that flips join runs; 9 allows only one flip and 8 none.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic