Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array a = [2, 1, 5, 2, 3, 1, 4] has only positive values. How many non-empty contiguous subarrays have sum at most 9?
- A. 17
- B. 10
- C. 28
- D. 11
Correct answer: A. 17
Positive values make a sliding window valid: for each right end r, r − l + 1 subarrays end at r, totalling 17. Adding r − l instead (forgetting the + 1) gives 10, counting all 28 subarrays ignores the bound, and counting the subarrays that exceed 9 gives 11.
More Coding Interview Problems questions
- 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…
- How many permutations of (1, 2, 3, 4, 5) have every pair of adjacent entries differing by at least 2? Solve with a bitmask DP that…
- How many integers x with 0 <= x <= 1000 have exactly four 1-bits in their binary representation?
- Among the values [3, 10, 5, 25, 2, 8], what is the maximum value of x XOR y over two different elements x and y?
- From the values [5, 12, 7, 3, 9, 14, 2, 8], what is the minimum number of values you must choose so that their sum is at least 25? Meet in…