Data Structures & AlgorithmsHardGATE CS · TCS NQT

For the problem 'choose the largest set of non-overlapping intervals', which greedy rule is correct?

  1. A. Sort by starting time and take each interval that does not overlap the previous one
  2. B. Sort by finishing time and take each interval that starts after the last one taken finished
  3. C. Sort by length, shortest first, and take every interval that does not overlap anything taken
  4. D. Sort by starting time and take the interval with the longest length first

Correct answer: B. Sort by finishing time and take each interval that starts after the last one taken finished

Finishing-time order leaves the most room for later intervals (exchange argument). Start-time order can choose one long early interval that blocks several short ones, and length-based rules can fail the same way.

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

Exams that ask this topic