Data Structures & AlgorithmsHardAmazon · Microsoft coding round

Problem: Activity selection (pick the maximum number of non-overlapping intervals from a set) is solved optimally by which greedy strategy?

  1. A. Sort by duration and pick the shortest activities first
  2. B. Try every subset and pick the best (brute force)
  3. C. Sort by finish time and greedily pick the next activity whose start is not before the previous pick's finish
  4. D. Sort by start time and always pick the earliest starting activity

Correct answer: C. Sort by finish time and greedily pick the next activity whose start is not before the previous pick's finish

Sorting by finish time and greedily taking any activity compatible with the last chosen one is a classic exchange-argument-provable optimal greedy strategy, running in O(n log n).

More Coding Interview Problems questions

Exams that ask this topic