Task scheduler with cooldown
Python data engineering interview problem. Difficulty: intermediate. Pattern: Heaps. About 16 minutes. Part of the Pro drill bank.
Calculate the minimum time intervals needed to execute all tasks with a mandatory cooldown between identical tasks. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Implement least_interval(tasks: list[str], n: int) -> int to find the minimum time intervals required to finish all CPU tasks. Each task is represented by an uppercase English letter and requires 1 time unit to complete. Between any two executions of the identical task, there must be at least n units of cooldown time where the CPU either runs other tasks or sits idle. Return the total number of CPU intervals (execution plus idle slots) required.
Requirements
- All tasks must be completed.
- The gap between any two identical tasks must be at least n.
Constraints
- 1 <= len(tasks) <= 10,000
- tasks[i] is an uppercase English letter.
- 0 <= n <= 100
Examples
Input: least_interval(['A', 'A', 'A', 'B', 'B', 'B'], 2) Output: 8 A sequence like A -> B -> idle -> A -> B -> idle -> A -> B requires 8 intervals.
Topics: lakebench, python, priority-queue, idle-slots.
More Python interview questions · All interview problems · Learn data engineering
Task scheduler with cooldown
Interview-style drill: Calculate the minimum time intervals needed to execute all tasks with a mandatory cooldown between identical tasks.
Implement `least_interval(tasks: list[str], n: int) -> int` to find the minimum time intervals required to finish all CPU tasks. - Each task is represented by an uppercase English letter and requires 1 time unit to complete. - Between any two executions of the identical task, there must be at least `n` units of cooldown time where the CPU either runs other tasks or sits idle. - Return the total number of CPU intervals (execution plus idle slots) required.