Task Scheduler
On this page
Problem statement
A CPU must run a list of tasks, each labelled with a letter A–Z; every task takes one time unit. Tasks may run in any order, but two runs of the same letter must have at least
ntime units between them, during which the CPU runs other tasks or sits idle. Return the minimum number of time units needed to finish all tasks.
Original problem: LeetCode 621. It shows up both as a heap simulation question and as a counting question with a neat closed formula; strong candidates know both and can explain why the formula is right.
Example

Input: tasks = [A, A, A, B, B, C], n = 2
Output: 7 # A B C A B idle A
Input: tasks = [A, A, A, B, B, B, C, C, D, D], n = 2 -> 10 # no idle needed
Input: tasks = [A, A, A, A], n = 3 -> 13
Constraints (assumed)
- 1 ≤ number of tasks ≤ 10⁴; letters A–Z; 0 ≤ n ≤ 100.
What this question tests
- Seeing that only the most frequent tasks force idle time.
- Turning that into a formula, including the case where no idle time is needed.
- Alternatively, a correct greedy simulation with a max-heap and a cooldown queue.
Questions to ask first
- Can tasks run in any order? (Here: yes.)
- Does n mean n slots in between, or n time units after? (Here: at least n other slots between two runs of the same task.)
- Do I return the length or the schedule? (Here: the length.)
- Can n be 0? (Yes; then the answer is the number of tasks.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.