The "Minimum Initial Energy to Finish Tasks" interview question is a sophisticated scheduling problem that combines element processing with resource constraints. You are given a list of tasks, where each task is defined by two values: the actual energy required to complete the task and the minimum energy required to start the task.
The rule is that you can only begin a task if your current energy is greater than or equal to its minimum requirement. Once you finish the task, your energy decreases by the actual amount. The goal is to determine the minimum initial energy you need to have at the very beginning to complete all given tasks in any order you choose.
Companies like Microsoft and Google use this problem to test a candidate's ability to apply Greedy Algorithms and Sorting to optimization problems. It's not immediately obvious in what order the tasks should be performed to minimize the starting energy.
Key skills being evaluated:
The core pattern is Greedy with Custom Sorting.
The "trick" to this problem is realizing that tasks with a large difference between minimum and actual energy should be tackled first. This difference (minimum - actual) represents the amount of energy that must be "kept in reserve" but isn't actually consumed.
By sorting tasks in descending order of this difference (minimum - actual), we ensure that we use our high initial energy to satisfy the most demanding "starting" requirements while we still have enough "buffer" energy available.
Suppose we have two tasks:
actual = 4, minimum = 10 (Difference = 6)actual = 8, minimum = 12 (Difference = 4)Order 1: A then B
Order 2: B then A (Correct Greedy Order, diff 4 is less than 6) Wait, let's check: Task A diff is 6, Task B diff is 4. Sorting by difference descending means A then B. Let's check if 16 works. Start with 16.
What if we started with B?
Whenever you encounter a problem where you need to "pick an order" to minimize or maximize something, think "Greedy with Sorting." Try to find a formula for comparing two items. Ask yourself: "If I have Task 1 and Task 2, under what condition is better than ?"