Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Array [2, 7, 4, 9, 5, 8]. What is the largest sum of a contiguous subarray having at most 3 elements?
- A. 26
- B. 19
- C. 35
- D. 22
Correct answer: D. 22
Window sums of length 1, 2 and 3 cover all candidates; the best is a window of three large values. Allowing longer windows would include the total sum, which is the wrong upper bound.
More Coding Interview Problems questions
- Points (0,0), (1,2), (2,1), (3,3), (4,0) in the plane. How many unordered pairs have Manhattan distance exactly 3?
- Four sets {1,2,3}, {2,4}, {3,4,5}, {1,5} are given. How many non-empty sub-collections of these sets have a union equal to {1, 2, 3, 4, 5}?
- Problem: How many pairs of equal values (i < j, a[i] = a[j]) does a = [1, 2, 1, 3, 2, 2] contain? Run the code. python from collections…
- Problem: How many longest increasing subsequences does [1, 3, 5, 4, 7] have? Run the DP that counts both length and number of optimal…
- Problem: What is the largest number of distinct characters in any window of length 3 of 'abcabc'? Run the code. python s = 'abcabc'…
- Problem: How many distinct values does x XOR y take over pairs of distinct elements of [1, 2, 3]? Run the code. python from itertools…