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

Sorted arrays A = [1, 7, 12, 20, 31] and B = [3, 9, 15, 25, 40]. What is the smallest value of |x - y| over x in A and y in B?

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

Correct answer: C. 2

Walk both arrays with two pointers, always advancing the pointer at the smaller value, which visits every candidate closest pair. The closest pairs are (1, 3) and (7, 9), both differing by 2, and comparing only the first elements of each array misses (7, 9).

More Coding Interview Problems questions

Exams that ask this topic