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?

  1. A. 27
  2. B. 23
  3. C. 26
  4. 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

Exams that ask this topic