Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

Sorted arrays A = [1, 4, 6, 9] and B = [2, 3, 7, 10]. How many pairs (x from A, y from B) have 8 <= x + y <= 12?

  1. A. 7
  2. B. 8
  3. C. 6
  4. D. 4

Correct answer: A. 7

For each x, the valid y values lie in a contiguous block of sorted B, found with two pointers or binary search; counting the block bounds gives the answer. Using open bounds drops sums exactly equal to 8 or 12.

More Coding Interview Problems questions

Exams that ask this topic