Task Scheduler with Cooldown
Updated
Problem statement
You are given an array tasks of task type IDs and an integer cooldown. Each task takes one time unit on a single processor.
You may run the tasks in any order. In each time unit, run one task or leave the processor idle. Two tasks with the same type ID must have at least cooldown intervening time units. Other task types and idle time both count toward that gap.
Return the minimum total time needed to finish every task. Do not include any idle time after the last task finishes.
Function
leastInterval(tasks: int[], cooldown: int) → intExamples
Example 1
tasks = [1,1,1,2,2]cooldown = 2return = 7One optimal schedule is 1, 2, idle, 1, 2, idle, 1. Type 1 runs at times 0, 3, 6, with two intervening time units between repeats. Its three occurrences require at least seven time units, and this schedule achieves that bound.
Example 2
tasks = [1,1,2,2,3,3]cooldown = 1return = 6The schedule 1, 2, 3, 1, 2, 3 finishes all six tasks without idle time. Every repeated type has two intervening tasks, which satisfies a cooldown of one. Six time units are necessary because only one task can run at a time.
Example 3
tasks = [7,7,7,7]cooldown = 0return = 4A cooldown of zero allows consecutive occurrences of the same type. Run all four tasks in four time units.
Constraints
1 <= tasks.length <= 10^4.1 <= tasks[i] <= 26.0 <= cooldown <= 100.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Nickname-Constrained GroupingZoox · ONSITE INTERVIEW
- Partition Into K-Size Consecutive GroupsDream11 · ONSITE INTERVIEWDream11 interview questions
- Minimum Baskets with Distinct Fruit TypesAmazon · ONSITE INTERVIEW
- Task Scheduler with CooldownSambaNova Systems · PHONE SCREEN · ONSITE INTERVIEWSambaNova Systems interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Minimum Distinct Prefix CostChubb · OAChubb interview questions
- Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
- Task Scheduler with CooldownTeradata · PHONE SCREEN
- Rearrange Songs by AuthorZomato / Eternal · OAZomato / Eternal interview questions
- 🐘 Keep Them Apart 🦙QRT · OA