Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array [3, 1, 2, 5, 0, 4, 2]. How many contiguous subarrays have a sum equal to twice their length?
- A. 8
- B. 5
- C. 1
- D. 7
Correct answer: D. 7
Subtract 2 from every element; the condition becomes that the shifted subarray has zero sum, which prefix sums with a hash map count in linear time. Forgetting the shift and testing equality with the length solves a different problem.
More Coding Interview Problems questions
- Array [1, 3, 5, 4, 2, 6, 8, 7, 9]. What is the length of the shortest contiguous segment that, once sorted, makes the whole array sorted?
- How many permutations of (0, 1, 2, 3) have exactly one position i with p[i] = i?
- Arrays A = [2, 5, 3, 7] and B = [4, 6, 1, 9]. How many pairs (x from A, y from B) satisfy 10 <= x y <= 40?
- Trains arrive at 100, 140, 150, 180, 200 and depart at 120, 160, 190, 210, 230 (times are not paired). A platform holds one train at a…
- Array [1, 2, 1, 3, 2, 2]. How many contiguous subarrays contain exactly two distinct values?
- Sorted arrays [2, 6, 9, 14] and [4, 7, 11, 15] are merged into one list. How many elements of the merged list lie strictly between 5 and 12?