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

Intervals [1,6), [2,4), [3,8), [5,7), [6,9), [7,10) are given. How many unordered pairs of these intervals overlap (share at least one common point of positive length)?

  1. A. 11
  2. B. 18
  3. C. 5
  4. D. 9

Correct answer: D. 9

Half-open intervals overlap exactly when max(start) < min(end), which holds for 9 pairs. Using ≤ also counts pairs that only touch at an endpoint, such as [1,6) and [6,9) (11), counting ordered pairs gives 18, and checking only neighbours after sorting gives 5.

More Coding Interview Problems questions

Exams that ask this topic