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

For the string s = "aabbabbaab", find the length of the longest substring with exactly as many letter a as letter b.

  1. A. 8
  2. B. 10
  3. C. 4
  4. D. 16

Correct answer: B. 10

Map a → +1, b → −1 and store the FIRST index of each prefix balance, seeded with balance 0 at index −1; here the whole string has 5 a's and 5 b's, so the answer is 10. Forgetting the seed misses prefixes that start at index 0 (8), overwriting with the latest index gives 4, and counting balanced substrings instead of measuring them gives 16.

More Coding Interview Problems questions

Exams that ask this topic