Total Run Time of Ordered Tasks with a Per-Type Cooldown
Company: Waymo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Overview: Tasks labeled by type must run in a fixed order, each taking one time unit, and two tasks of the same type need a cooldown gap between them. Compute the total time until the last task finishes. It tests careful simulation, per-type bookkeeping and edge cases such as an empty input or a zero cooldown.
Constraints
- 0 <= len(tasks) <= 10^5
- tasks consists of uppercase English letters 'A' to 'Z'.
- 0 <= cooldown <= 10^4
- The answer is at most 1,000,090,000, so it fits in a 32-bit signed integer.
Examples
Input: ('', 5)
Expected Output: 0
Explanation: Empty task list: nothing runs, so the answer is 0.
Input: ('Q', 10000)
Expected Output: 1
Explanation: Single task with the maximum cooldown finishes at time 1; the cooldown never applies.
Hints
- Tasks cannot be reordered, so walk through them in the given order and decide only when each one starts.
- A task's earliest start depends on when the previous task in the string finished and on when the most recent task of the same type finished. What do you need to remember to know the second one?
- Check the boundary: if a task of type X finished at time f, starting the next X at exactly f + cooldown is allowed.