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?
- A. Sort by duration and pick the shortest activities first
- B. Try every subset and pick the best (brute force)
- C. Sort by finish time and greedily pick the next activity whose start is not before the previous pick's finish
- 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
- Problem: Which of these coin systems is a classic counterexample where the greedy 'always take the largest coin' algorithm fails to find…
- Problem: Huffman coding builds an optimal prefix-free binary code for a set of symbol frequencies. Which greedy step does it repeat?
- Problem: What is the time complexity of computing the longest increasing subsequence using patience-sorting with binary search, versus the…
- Problem: The longest common subsequence (LCS) of two strings of length n and m is typically solved with which approach and complexity?
- Problem: Subset-sum (does any subset of n integers sum exactly to a target T?) has a DP solution running in O(nT). Why is this not…
- Problem: What is the average-case and worst-case time complexity of the quickselect algorithm for finding the k-th smallest element?