Minimize time for two handlers
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
Quick Answer: This question evaluates algorithmic optimization skills and stateful scheduling reasoning, focusing on task assignment trade-offs and cumulative cost minimization for two handlers.
Part 1: Minimum Preparation Time for Two Handlers
Constraints
- 0 <= len(workList) <= 200
- 0 <= len(longPrepTime) == len(shortPrepTime) <= 50
- If workList is non-empty, every work type x satisfies 1 <= x <= len(longPrepTime)
- 0 <= longPrepTime[i], shortPrepTime[i] <= 10^4
Examples
Input: ([1, 2, 2], [4, 5], [2, 3])
Expected Output:
Explanation: Assign work 1 to handler A for 4, work 2 to handler B for 5, and the last work 2 again to handler B for 3. Total = 12.
Input: ([], [4, 5], [2, 3])
Expected Output:
Explanation: No jobs means no preparation cost.
Input: ([1, 1, 1], [7], [2])
Expected Output:
Explanation: Best is to keep all three jobs on the same handler: 7 + 2 + 2 = 11.
Input: ([1, 2, 1, 2], [5, 6], [1, 1])
Expected Output:
Explanation: Let one handler keep type 1 and the other keep type 2: 5 + 6 + 1 + 1 = 13.
Input: ([1, 2, 3], [3, 4, 5], [1, 1, 1])
Expected Output:
Explanation: All three jobs are effectively long preparations because no handler can repeat the same type here. Total = 3 + 4 + 5 = 12.
Hints
- The full assignment history does not matter. For each handler, only its last job type affects the next cost.
- Use dynamic programming over the prefix of workList and the pair of last job types for the two handlers.
Part 2: Replacement Problem - Balance Jobs Between Two Handlers
Constraints
- 0 <= len(jobs) <= 200
- 1 <= jobs[i] <= 1000 for each job
- sum(jobs) <= 20000
Examples
Input: [3, 1, 4, 2, 2]
Expected Output:
Explanation: One optimal split is {4, 2} and {3, 2, 1}, both summing to 6.
Input: []
Expected Output:
Explanation: No jobs means the finishing time is 0.
Input: [7]
Expected Output:
Explanation: A single job must be handled by one handler, so the makespan is 7.
Input: [1, 2, 3, 9]
Expected Output:
Explanation: Best split is {9} and {1, 2, 3}. The larger side is 9.
Input: [5, 5, 5, 5]
Expected Output:
Explanation: Split into two pairs: 10 and 10.
Hints
- If one handler gets total time s, the other gets total time sum(jobs) - s. You want these two sums as close as possible.
- A subset-sum dynamic programming approach up to half of the total duration is enough.