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

Array a = [2, 1, 3, 4, 1, 2, 5, 1]. How many non-empty contiguous subarrays contain exactly as many odd numbers as even numbers?

  1. A. 9
  2. B. 11
  3. C. 8
  4. D. 13

Correct answer: A. 9

Map odd to +1 and even to -1; a subarray is balanced exactly when two prefix balances are equal. Counting equal pairs with a hash map seeded at balance 0 gives the total, and the seed must be included to count subarrays starting at index 0.

More Coding Interview Problems questions

Exams that ask this topic