Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Sorted arrays A = [3, 8, 12, 17, 25] and B = [5, 9, 14, 20]. Choose one element from A and one from B so that the sum is as large as possible without exceeding 26. What is that sum?
- A. 27
- B. 23
- C. 26
- D. 45
Correct answer: C. 26
Two pointers: start with the smallest of A and the largest of B, and move the pointer that keeps the sum within the limit as far as possible. The best pair is 12 + 14 = 26, while the global maximum pair exceeds the limit.
More Coding Interview Problems questions
- Balloons span closed integer intervals [1,4], [2,3], [4,6], [5,8], [7,9]. One arrow shot at integer x bursts every balloon whose interval…
- How many binary strings of length 6 contain neither two consecutive 0s nor three consecutive 1s?
- How many subsets (including the empty subset) of [3, 5, 9, 11, 14, 2] have a sum divisible by 7?
- Closed intervals [1,4], [2,6], [4,7], [5,9], [8,10] are given. What is the largest number of intervals that share a single integer point?
- Array [2, 8, 9, 18, 4, 16, 6, 3]. How many index pairs i < j have a[i] a[j] equal to a perfect square?
- Array [1, 2, 4, 3, 0, 5, 6]. What is the length of the longest contiguous subarray in which the XOR of the elements equals their sum?