Task Scheduler
Problem Statement
A CPU has a list of tasks. Each task is one letter from A to Z. In each time unit the CPU either runs one task or sits idle. The same task can't run again until at least n units have passed since its last run.
Return the least number of time units needed to finish every task.
An array tasks of uppercase letters, and an integer n, the cooldown.
One integer: the minimum number of time units to run all the tasks.
A → B → idle → A → B → idle → A → B. Each letter waits two units before it runs again.
A → B → C → D → A → B. A cooldown of 1 is already met by the other tasks.
With no cooldown, the tasks just run back to back.
- • 1 ≤ tasks.length ≤ 10^4
- • Each task is an uppercase English letter.
- • 0 ≤ n ≤ 100
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Each time unit, scan all 26 letters for the most frequent one that is not cooling down; idle if none is ready
Ready tasks in a max-heap; a task that just ran waits in a queue until its cooldown ends, then returns to the heap
Only the top count and how many tasks share it matter; the answer is that frame length, but never less than the number of tasks
Simulate the CPU one unit at a time. At each unit, check all 26 letters and run the one with the most copies left among those that are ready. If none is ready, the unit is idle. Stop when no copies are left.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.