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?
- A. 9
- B. 11
- C. 8
- 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
- Array a = [4, -1, 2, -3, 5, -2, 1, 1, -4]. How many DISTINCT values appear among the sums of all non-empty contiguous subarrays?
- Array a = [6, 1, 8, 3, 9, 4, 2, 7, 5] has distinct values. How many ordered pairs (i, j) with i != j satisfy a[i] - a[j] = 4?
- For a = [1, 2, 3, 4, 5, 6, 7, 8], how many contiguous subarrays have a sum that is a multiple of 5?
- Array a = [2, 1, 5, 2, 3, 1, 4] has only positive values. How many non-empty contiguous subarrays have sum at most 9?
- Sorted values [1, 4, 6, 9, 13, 14, 18, 22] must be split into the fewest contiguous groups so that in every group the largest value minus…
- Two multisets A = {7, 2, 9, 4, 5} and B = {3, 8, 1, 6, 10} are matched one-to-one, pairing each element of A with exactly one element of…