Data Structures & AlgorithmsModerateProduct-company MCQ pattern
In the activity selection problem (choose the most non-overlapping intervals), which greedy rule always gives an optimal answer?
- A. Pick the activity with the fewest overlaps
- B. Pick the shortest activity first
- C. Pick the activity that finishes earliest, then repeat on the compatible ones
- D. Pick the activity that starts earliest
Correct answer: C. Pick the activity that finishes earliest, then repeat on the compatible ones
Taking the earliest finish leaves the most room for the rest, and an exchange argument shows it never hurts. Earliest start and shortest first both have easy counterexamples.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Why does the greedy method (best value-per-weight first) solve the fractional knapsack problem but not the 0/1 knapsack problem?
- How many edges does a complete undirected simple graph on 10 vertices have?
- What is the time to search for a word of length L in a trie that holds n words?
- An undirected graph has 12 vertices and the sum of the degrees of all vertices is 40. How many edges does it have?
- The preorder traversal of a BST is 8, 5, 1, 7, 10, 12. What is its postorder traversal?
- A binary tree has inorder 4 8 2 5 1 6 3 7 and preorder 1 2 4 8 5 3 6 7. What is its postorder?