Task Scheduler with Cooldown
Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The SambaNova Systems OA, reported in June 2022, hands you a task scheduler with a cooldown, and the whole thing hinges on a max-heap plus a cooldown queue. If you've seen LeetCode's Task Scheduler, you're ahead. Each task takes one time unit, same IDs need a gap, and you return the minimum total time. The twist here is that task IDs are strings up to 20 characters, not single letters, so don't hardcode a 26-slot array. You've got an invite and a clock, so here's the shape of the answer. StealthCoder is the invisible backup if your mind goes blank mid-assessment.
The problem
Each task takes one time unit. You may execute tasks in any order or remain idle, but two executions of the same task ID must have at least cooldown intervening time units. Return the minimum total number of time units needed to execute every task. Function leastInterval(tasks: String[], cooldown: int) → int Examples Example 1 tasks = ["A","A","A","B","B","B"] cooldown = 2 return = 8 A B idle A B idle A B is optimal. Example 2 tasks = ["A","A","A","B","B","B"] cooldown = 0 return = 6 No cooldown requires no idle time. Example 3 tasks = ["A","A","A","B","B","B","C","C"] cooldown = 2 return = 8 C tasks fill both idle positions. Constraints 1 <= tasks.length <= 100000. Task IDs are non-empty uppercase ASCII strings of at most 20 characters. 0 <= cooldown <= 100000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is greedy. Always run the task with the highest remaining count, because the most frequent task is what creates idle gaps. Count frequencies in a hash map, push counts into a max-heap, and keep a queue of tasks cooling down with the time they become available again. Each tick, release any ready task back into the heap, pop the top, decrement, and queue it if any remain. If the heap is empty but the queue isn't, jump time forward. There's also a closed form: (maxFreq - 1) * (cooldown + 1) + numberOfTasksWithMaxFreq, then take the max with tasks.length. Common pitfall: using chars instead of full string IDs, and forgetting cooldown = 0 returns tasks.length. With up to 100000 tasks, simulate by jumping time, not by ticking idle slots one by one. StealthCoder sits invisibly as a safety net if you freeze on the heap-versus-formula choice.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Task Scheduler with Cooldown cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as task scheduler. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass SambaNova Systems's OA.
SambaNova Systems reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Task Scheduler with Cooldown FAQ
What's the trick to Task Scheduler with Cooldown?+
Greedy on frequency. Always schedule the task with the most remaining runs, since it forces the idle gaps. Use a max-heap for counts and a queue for tasks waiting out their cooldown. Or use the formula (maxFreq - 1) * (cooldown + 1) + countOfMax, capped below by tasks.length.
Do I need the heap or is the formula enough?+
The formula is enough for the minimum time and runs in linear time. The heap simulation is the fallback if you forget the formula or the interviewer wants the actual schedule. Know both. Formula first, simulation to verify on the examples.
How is this different from the LeetCode version?+
Task IDs here are strings up to 20 characters, not single letters, so use a hash map for counts instead of a fixed 26-length array. Cooldown can also go up to 100000, so don't build the schedule slot by slot with a naive loop.
What edge cases break most solutions?+
Cooldown of 0 should return tasks.length. A single task type with many repeats creates lots of idle time. Multiple tasks tied for max frequency add to the final count. Always take the max of the formula result and tasks.length so you never undercount.
How do I prepare in 48 hours for this OA?+
Write the heap-plus-queue version once from scratch, then the formula version. Test against the three examples, especially example 3 where C fills the idle gaps. Practice swapping chars for string keys in the hash map. That covers most of what this problem tests.