Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array a = [2, -1, 3, 1, -2, 4]. How many contiguous subarrays have a sum equal to their length? Subtract 1 from each element first and count zero-sum subarrays with prefix sums.
- A. 7
- B. 6
- C. 4
- D. 0
Correct answer: B. 6
Subtracting 1 from every element turns the condition into sum(a_i - 1) = 0 over the subarray, and then prefix sums with a hash map count the zero-sum windows. Counting zero-sum subarrays of the original array is a different quantity.
More Coding Interview Problems questions
- Sorted arrays A = [1, 5, 9, 12] and B = [3, 4, 10, 15, 20]. How many pairs (x in A, y in B) satisfy |x - y| <= 2?
- Array a = [1, 2, 1, 3, 2, 2, 4, 1]. What is the length of the longest contiguous subarray that contains at most two distinct values?
- Sorted array a = [2, 3, 5, 6, 8, 10]. How many DISTINCT values appear among the sums a[i] + a[j] over pairs i < j?
- Sorted array a = [1, 2, 4, 5, 6, 9, 10, 11]. How many elements does the largest contiguous block of this array have if its largest element…
- How many permutations (p1, ..., p5) of the numbers 1 to 5 satisfy pi <= i + 1 for every position i? Use a bitmask DP over the set of…
- Choose any subset (empty allowed) of {1, 2, 3, 4, 5}. How many subsets have bitwise OR equal to 7?