Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array a = [2, 1, -1, 4, -2, 3, 0, 1, 1, 0, 2]. How many indices i have the sum of the elements strictly left of i equal to the sum of the elements strictly right of i? Element a[i] itself is excluded from both sides.
- A. 0
- B. 1
- C. 2
- D. 5
Correct answer: B. 1
Let P be the prefix sum. The condition is P[i] = total - P[i] - a[i], which excludes the pivot element. Including a[i] on one side is the common error and changes the count.
More Coding Interview Problems questions
- Array a = [3, 1, 2, 6, 4, 5, 9]. How many index pairs (i < j) satisfy a[i] mod 3 = a[j] mod 3?
- Sorted array a = [-4, -1, 0, 2, 3, 5, 7]. How many triples of distinct indices i < j < k have a[i] + a[j] + a[k] <= 6?
- For the positive array a = [2, 3, 1, 2, 4, 3] and S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?
- Sorted array a = [1, 3, 4, 7, 10, 12, 15]. How many pairs of indices i < j satisfy a[j] - a[i] <= 3?
- Given the array [-1, 2, 1, -4, 6, 3] and target 1, pick three different elements whose sum is as close to 1 as possible. What is that…
- Heights h = [1, 8, 6, 2, 5, 4, 8, 3, 7, 9] are drawn as vertical lines at positions 0 to 9. What is the largest amount of water that two…