Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

Closed intervals [1,3], [3,5], [2,4], [5,7], [6,8] are given. Two intervals conflict if they share any point, endpoints included. What is the maximum number of pairwise non-conflicting intervals?

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

Correct answer: B. 2

Sort by right endpoint and take an interval when its start is strictly after the last chosen end, since touching endpoints conflict for closed intervals. Allowing touching ends (start equal to the last end) overcounts, which is the trap.

More Coding Interview Problems questions

Exams that ask this topic