621. Task Scheduler
Asked at Google, Meta, Amazon, Microsoft and 2 more
Difficulty
Medium
Time
O(m)
Space
O(1)
Pattern
Greedy
Companies
6
Problem
You are given a list of CPU tasks. Each task is a single uppercase letter, and each task takes exactly one CPU interval to run. The CPU can also stay idle for an interval.
There is one rule: between two runs of the same task label there must be at least n other intervals (tasks or idle). You may run the tasks in any order you like.
Return the smallest number of intervals (task runs plus idle intervals) needed to finish every task.
Same task needs a gap of n
Constraints
- ●
1 <= tasks.length <= 10^4 - ●
tasks[i] is an uppercase English letter (A-Z) - ●
0 <= n <= 100
Examples
tasks = ["A","A","A","B","B","B"], n = 28tasks = ["A","C","A","B","D","B"], n = 16tasks = ["A","A","A","B","B","B"], n = 310Asked At
6 companies| Company | Difficulty | |
|---|---|---|
| Medium | View all Google questions → | |
| Meta | Medium | View all Meta questions → |
| Amazon | Medium | View all Amazon questions → |
| Microsoft | Medium | View all Microsoft questions → |
| Apple | Medium | View all Apple questions → |
| Medium | View all LinkedIn questions → |
How to Think About It
The most frequent task determines the minimum length. Calculate idle slots based on the max frequency.
Greedy: schedule the most frequent tasks first, then fill gaps with other tasks.
Formula: (maxFreq - 1) × (n + 1) + countOfMaxFreq. This accounts for the cooldown gaps between the most frequent tasks.
If there are enough different tasks to fill all idle slots, the answer is simply len(tasks). No idle time needed.
Edge cases: n=0 (return len(tasks)), single task type, all unique tasks.
Optimal Approach
Step 1: Count task frequencies.
Step 2: Find max frequency (maxFreq) and count of tasks with that frequency.
Step 3: Return max(len(tasks), (maxFreq - 1) × (n + 1) + countOfMaxFreq).
The max ensures we don't undercount when there are enough tasks to avoid idle time.
Time: O(n) — count frequencies. Space: O(1) — at most 26 task types.
Count the most frequent task
What Trips People Up in Real Interviews
Confusing this with "rearrange string so no two adjacent are same." That's a different problem (rearrange string). This one has a cooldown constraint.
Not using the formula. The minimum intervals = max(len(tasks), (maxFreq - 1) × (n + 1) + countOfMaxFreq). Don't simulate the scheduling.
Forgetting that the answer is at least len(tasks). If there are enough tasks to fill all gaps, no idle time is needed.
Not handling the case where n = 0. Return len(tasks) — no cooldown needed.
Miscounting tasks with max frequency. If multiple tasks share the max frequency, they all go in the last "frame." The formula adds countOfMaxFreq, not 1 — forgetting this underestimates the result.
Solution Code
O(m) timefrom collections import Counter
def leastInterval(tasks, n):
freq = Counter(tasks)
max_freq = max(freq.values())
max_count = sum(1 for v in freq.values() if v == max_freq)
return max(len(tasks), (max_freq - 1) * (n + 1) + max_count)Frequently Asked Questions
What is the Task Scheduler problem?
Given a char array representing CPU tasks and a cooldown interval n, return the least number of intervals the CPU will take to finish all tasks. Each task takes one interval. The same task must have at least n intervals between its occurrences.
How do you solve Task Scheduler?
The optimal approach runs in O(m) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Task Scheduler?
Task Scheduler is asked at Google, Meta, Amazon, Microsoft, Apple, LinkedIn. It is a medium difficulty problem.
What are common mistakes on Task Scheduler?
- Confusing this with "rearrange string so no two adjacent are same." That's a different problem (rearrange string). This one has a cooldown constraint.
- Not using the formula. The minimum intervals = `max(len(tasks)`, (maxFreq - 1) × (n + 1) + countOfMaxFreq). Don't simulate the scheduling.
- Forgetting that the answer is at least len(tasks). If there are enough tasks to fill all gaps, no idle time is needed.
- Not handling the case where `n = 0`. Return len(tasks) — no cooldown needed.
- Miscounting tasks with max frequency. If multiple tasks share the max frequency, they all go in the last "frame." The formula adds countOfMaxFreq, not 1 — forgetting this underestimates the result.