Data Structures & AlgorithmsHardGATE CS · TCS NQT
For the problem 'choose the largest set of non-overlapping intervals', which greedy rule is correct?
- A. Sort by starting time and take each interval that does not overlap the previous one
- B. Sort by finishing time and take each interval that starts after the last one taken finished
- C. Sort by length, shortest first, and take every interval that does not overlap anything taken
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A DP over prefixes of an array computes the number of ways to split it into segments where each segment's sum is at most L. Which…
- Which problem is the standard application of 'binary search on the answer' where the feasibility test itself is a greedy scan?
- In bitmask DP over subsets of n items, what is the typical total time for the TSP-style recurrence dp[mask][last] with transitions to…
- Which statement about the 'house robber' DP on a line is correct?
- A greedy solution to a scheduling problem sorts jobs by profit and places each into the latest free slot before its deadline. What must…
- A coin-change problem uses coins {1, 3, 4} and asks for the minimum number of coins for each amount. Which statement is correct?