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

Values [1, 9, 4, 12, 7, 15, 3]. How many unordered pairs of distinct positions have an absolute difference of at least 5?

  1. A. 11
  2. B. 6
  3. C. 13
  4. D. 8

Correct answer: C. 13

Of the 21 pairs, 8 differ by less than 5, so 13 differ by at least 5 (sorting plus two pointers counts them in O(n log n)). Using > 5 drops the pairs differing by exactly 5 (11), checking only adjacent positions gives 6, and 8 counts the complement.

More Coding Interview Problems questions

Exams that ask this topic