Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
For a = [2, -1, 4, -3, 5], which of the following statements is TRUE? A. Some contiguous subarray sums to 7. B. Every prefix sum from the second one onward is at least 2. C. The maximum subarray sum is 9. D. No contiguous subarray sums to 0.
- A. Some contiguous subarray sums to 7.
- B. Every prefix sum from the second one onward is at least 2.
- C. The maximum subarray sum is 9.
- D. No contiguous subarray sums to 0.
Correct answer: A. Some contiguous subarray sums to 7.
Prefix sums are 0, 2, 1, 5, 2, 7. The subarray [2, -1, 4, -3, 5] sums to 7, which is the only true statement; the minimum prefix from the second onward is 1, the maximum subarray sum is 7 rather than 9, and the subarray [4, -3, 5] style checks reveal a zero sum from -1 + 4 - 3.
More Coding Interview Problems questions
- What does this program print? python a = [1, -1, 2, -2, 3] seen = {0: 0} s = 0; out = [] for i, x in enumerate(a, 1): s += x if s in seen…
- What does this program print? python best = None s = 0 for x in [4, -2, -2, 3, -1]: s += x best = s if best is None else max(best, s)…
- Array [1, 3, 2, 6, 4, 5, 7, 8]. How many inversions does it have (index pairs i < j with a[i] > a[j])?
- Sequence [5, 2, 9, 1, 7, 3]. Consider only index pairs i < j with j - i <= 2. What is the largest value of |a[i] - a[j]| over these pairs?
- What is the length of the longest strictly increasing subsequence of [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]? Use the patience-sorting method with…
- Sorted array [1, 2, 7, 8, 11, 15]. How many index triples i < j < k satisfy a[i] + a[j] + a[k] = 18?