Data Structures & AlgorithmsVery hardAmazon · Microsoft coding round
Problem: Which best explains why the greedy 'interval scheduling to maximize the number of non-overlapping intervals' problem is solved optimally by sorting on finish time, not on start time or duration?
- A. This problem actually requires DP, not greedy, to be correct
- B. Picking the interval that finishes earliest always leaves the most room for future choices, which an exchange argument proves is never worse than any other valid selection
- C. Sorting by duration always gives the optimal answer too
- D. Sorting by start time always gives the same answer as sorting by finish time
Correct answer: B. Picking the interval that finishes earliest always leaves the most room for future choices, which an exchange argument proves is never worse than any other valid selection
An exchange argument shows that any optimal solution can be transformed, without decreasing its size, into one that starts by picking the earliest-finishing compatible interval; sorting by start time or duration does not have this property and can lead to suboptimal choices.
More Coding Interview Problems questions
- Problem: Which is the correct worst-case time complexity for inserting into a balanced BST (like a Red-Black or AVL tree) with n nodes…
- Problem: What is the time complexity of the standard recursive merge sort, including the merge step, expressed via its recurrence T(n) =…
- Problem: Which statement about the time complexity of checking if two strings are anagrams is most efficient and correct?
- Problem: For the 'minimum number of platforms needed for a railway station given arrival and departure times' problem, what is the…
- Problem: Which correctly describes why a hash set gives O(1) average-case membership testing, but a balanced BST set gives O(log n)?
- Problem: Which of these correctly explains why BFS (not DFS) is used to find the shortest path in an unweighted graph?