Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA

Problem: Given n ≤ 10⁵ intervals, find the minimum number of points such that every interval contains at least one point. Which greedy rule is correct?

  1. A. Sort by end; place a point at the end of the first uncovered interval, skip all intervals containing it
  2. B. Sort by start; place a point at each interval's start
  3. C. Sort by length; place a point in the middle of the shortest
  4. D. Place points at every distinct endpoint

Correct answer: A. Sort by end; place a point at the end of the first uncovered interval, skip all intervals containing it

Choosing the earliest end is safe by an exchange argument: any solution must stab that interval somewhere at or before its end, and moving that point right to the end can only cover more. O(n log n). Starting points fail on nested intervals like [1,10], [2,3].

More Coding Interview Problems questions

Exams that ask this topic