Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA

In the binary string s = "1100110100", count the non-empty substrings that contain exactly as many 0s as 1s.

  1. A. 15
  2. B. 11
  3. C. 6
  4. D. 16

Correct answer: D. 16

Map 1 to +1 and 0 to -1. A balanced substring is a pair of equal prefix balances, so count equal pairs with a hash map seeded with balance 0. Forgetting that seed loses substrings that start at index 0.

More Coding Interview Problems questions

Exams that ask this topic