Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Starting from the string "00000" of five zeros, operations flip a substring: each operation chooses positions l ≤ r and flips every bit in between (0 becomes 1 and 1 becomes 0). The operations are: flip [0,2], flip [1,4], flip [2,2], flip [0,1]. How many 1s are in the final string?

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

Correct answer: B. 4

Each flip is an XOR on a range; simulating it bit by bit gives the final state. Counting only the cells touched once (ignoring the parity from overlapping flips) gives the wrong total.

More Coding Interview Problems questions

Exams that ask this topic