Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Array a = [-2, 5, -1, 4, -3, 6]. How many contiguous subarrays have a sum strictly between 0 and 10?
- A. 17
- B. 16
- C. 20
- D. 21
Correct answer: B. 16
Of the 21 subarray sums, 16 lie strictly between 0 and 10; the sums −3, −2, −1, 0 and 11 fail. Allowing a sum of 0 gives 17, dropping the lower bound gives 20, and 21 counts every subarray.
More Coding Interview Problems questions
- Sorted array [2, 3, 7, 9, 12, 15, 18]. How many index pairs i < j have a difference srt[j] - srt[i] in the range (3, 8], i.e. greater than…
- How many integers x with 1 <= x <= 30 have a binary representation with no two adjacent 1-bits?
- From the ten numbers [3, 8, 12, 5, 7, 10, 1, 4, 9, 6], how many 3-element subsets have a sum divisible by 5?
- A car starts at position 0 with 10 litres of fuel. It must reach position 40; it uses one litre per unit of distance. Gas stations at…
- From [5, 9, 2, 14, 7, 3, 8], keep as many values as possible so that the largest kept value is at most twice the smallest kept value. What…
- How many integers x with 0 <= x <= 255 have a number of 1-bits that is divisible by 3? (Zero has zero 1-bits, which is divisible by 3.)