Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT

Intervals (half-open, [start, end)) are [(1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8)]. What is the minimum number of intervals you must REMOVE so that the remaining intervals do not overlap (touching intervals such as [1, 3) and [3, 5) are allowed)?

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

Correct answer: D. 3

Keep the largest set of non-overlapping intervals (greedy by end time) and remove the rest; the answer is n minus that size. Removing intervals that overlap the most at a point can remove more than necessary.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic