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

Sorted arrays A = [1, 5, 9, 12] and B = [3, 4, 10, 15, 20]. How many pairs (x in A, y in B) satisfy |x - y| <= 2?

  1. A. 6
  2. B. 4
  3. C. 5
  4. D. 2

Correct answer: C. 5

With both arrays sorted, a pointer into B that advances past values too small for x gives each x its window of valid y in linear total time. A strict bound drops pairs that differ by exactly 2.

More Coding Interview Problems questions

Exams that ask this topic