Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: The intervals [1,3], [2,4], [3,6], [7,8], [5,9], [8,10] are given. What is the minimum number of intervals to remove so that the remaining ones are pairwise non-overlapping (touching at an endpoint is allowed)?

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

Correct answer: C. 2

Keep the maximum set of non-overlapping intervals by greedily choosing the earliest-ending interval; the removal count is the total minus that maximum. Removing the longest intervals first is not optimal.

More Coding Interview Problems questions

Exams that ask this topic