Coordinate workers across two exclusive targets
Company: Tesla
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in concurrent programming and synchronization, focusing on safe resource coordination, mutual exclusion, utilization maximization, and fault-tolerant handling of worker failures.
Constraints
- 0 <= n <= 100000
- len(setup_times) == len(run_times) == n
- 0 <= setup_times[i] <= 10^9
- 1 <= run_times[i] <= 10^9
Examples
Input: ([], [])
Expected Output: []
Explanation: There are no workers, so the schedule is empty.
Input: ([0, 0, 1], [5, 2, 3])
Expected Output: [(0, 'A', 0, 5), (1, 'B', 0, 2), (2, 'B', 2, 5)]
Explanation: Workers 0 and 1 are both ready at time 0, so A gets worker 0 and B gets worker 1. Worker 2 becomes ready at time 1 and starts on B as soon as B frees at time 2.
Input: ([0, 3, 3], [3, 2, 1])
Expected Output: [(0, 'A', 0, 3), (1, 'A', 3, 5), (2, 'B', 3, 4)]
Explanation: Worker 0 starts on A at time 0. At time 3, workers 1 and 2 are ready and both targets are free, so A is assigned first to worker 1 and then B to worker 2.
Input: ([0, 1, 1, 1], [4, 3, 2, 1])
Expected Output: [(0, 'A', 0, 4), (1, 'B', 1, 4), (2, 'A', 4, 6), (3, 'B', 4, 5)]
Explanation: Worker 1 takes the idle B at time 1. Workers 2 and 3 wait until time 4, when both targets free at once; tied setup times are broken by index, so worker 2 is assigned before worker 3.
Input: ([0, 0, 2, 4], [4, 4, 3, 1])
Expected Output: [(0, 'A', 0, 4), (1, 'B', 0, 4), (2, 'A', 4, 7), (3, 'B', 4, 5)]
Explanation: Worker 2 has been waiting since time 2. At time 4, both targets free and worker 3 becomes ready; worker 2 is chosen first for A, then worker 3 gets B.
Input: ([5], [2])
Expected Output: [(0, 'A', 5, 7)]
Explanation: Both targets are idle until time 5. With one ready worker and both targets free, target A is assigned first.
Hints
- Treat setup completions and target releases as time-ordered events. You do not need real threads for the judge version.
- Use one structure for workers that are ready to run, and another structure to track when each busy target becomes free.