Data Structures & AlgorithmsHardTCS NQT · Product Company SDE

Problem: In the binary string "0011100", what is the length of the longest contiguous substring that contains equal numbers of 0s and 1s?

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

Correct answer: A. 6

Prefix balances (+1 for 1, −1 for 0) repeat 6 apart, e.g. "001110" with three of each, so 6. "0011" gives 4, the whole string (7) has four 0s and three 1s, and 2 is a single "01".

More Coding Interview Problems questions

Exams that ask this topic