Question bankPricingSign in

Task Scheduler

Heaps & Priority QueuesHard1:30

You are given a list of tasks represented by characters and a cooldown period n. The same task must be separated by at least n intervals. Each interval is one unit of time, and the CPU can either execute a task or sit idle.

For example, given tasks = ["A","A","A","B","B","B"] and n = 2, one optimal schedule is A B idle A B idle A B, taking 8 intervals.

Explain your approach to finding the minimum number of intervals needed and state the time and space complexity.

How to approach it

  • Hint 1

    Think about which task constrains the schedule the most -- the one with the highest frequency.

  • Hint 2

    The most frequent task creates a frame of (maxFreq - 1) groups of size (n + 1), plus a final group for tasks with max frequency.

  • Hint 3

    The formula is: intervals = (maxFreq - 1) * (n + 1) + countOfMaxFreqTasks. But the answer is at least the total number of tasks.

Ready to answer it out loud?

Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.

Practising needs an invite code. Join the waitlist and we’ll send you one.