ProgrammingHardAccenture · Cognizant

A binary string s = "1100101" is to be made alternating. Two targets are possible: "0101010..." and "1010101...", each the same length as s. Flipping one bit costs 1. The minimum number of flips is printed.

A binary string s = "1100101" is to be made alternating. Two targets are possible: "0101010..." and "1010101...", each the same length as s. Flipping one bit costs 1. The minimum number of flips is printed.
flips0 = mismatches with pattern starting 0
flips1 = mismatches with pattern starting 1
Print min(flips0, flips1)
  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 2

Correct answer: D. 2

Against 1010101 the string has only 2 mismatches, and against 0101010 it has 5, so the minimum is 2. Using only the 0-start target gives 5, counting the ones gives 4, and counting the zeros gives 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic