Data Structures & AlgorithmsHardProduct Company SDE · prefix-count OA

Problem: What is the minimum number of bit flips needed to make the binary string '00110' non-decreasing (all 0s then all 1s)? Run the code.

Problem: What is the minimum number of bit flips needed to make the binary string '00110' non-decreasing (all 0s then all 1s)? Run the code.
s = '00110'
print(min(s[:k].count('1') + s[k:].count('0') for k in range(len(s) + 1)))
What is printed?
  1. A. 2
  2. B. 0
  3. C. 3
  4. D. 1

Correct answer: D. 1

Flipping the final 0 to 1 gives 00111, which is sorted with a single flip. Each split point k costs the 1s to its left plus the 0s to its right.

More Coding Interview Problems questions

Exams that ask this topic