Data Structures & AlgorithmsHardTCS NQT · Infosys

What is the length of the longest contiguous substring of the binary string "1100100110111000101" that has equally many 0s and 1s?

  1. A. 16
  2. B. 9
  3. C. 20
  4. D. 18

Correct answer: D. 18

Map each 1 to +1 and each 0 to −1 and look for the longest subarray with sum 0, which is a pair of equal prefix balances. Recording the LAST index for each balance instead of the first gives the shorter lengths.

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

Exams that ask this topic