Skip to main content
All Questions

Design a task scheduler in Python.

Medium

You are given a list of tasks where each task is represented by a character. Each task must be performed in a sequence, and you need to ensure that the same task is not executed again until after a cooldown period of n intervals. The goal is to determine the minimum number of intervals required to complete all tasks, including any idle time required to respect the cooldown period.

Write a function leastInterval that returns the minimum number of intervals required to execute all tasks while respecting the cooldown period n.

Examples

tasks = ['A', 'A', 'A', 'B', 'B', 'B'] n = 2 output: 8

To execute all tasks while respecting the cooldown period of 2, the schedule could be ['A', 'B', 'idle', 'A', 'B', 'idle', 'A', 'B']. This schedule allows each task to be performed and includes idle intervals where necessary to respect the cooldown period. Thus, the minimum number of intervals required is 8.

tasks = ['A', 'A', 'A', 'B', 'B', 'B'] n = 0 output: 6

With a cooldown period of 0, there is no idle time needed. Thus, the minimum number of intervals is equal to the number of tasks, which is 6.

Related courses

Course

AI Engineering Interview Prep

Ace the AI engineering interview at top companies like Google, Anthropic, Meta, and OpenAI. Learn from detailed frameworks, example questions, and mock interviews with senior engineers. Practice system design, AI/ML concepts, AI-assisted coding, and behavioral rounds.

Course

Generative AI Interviews

Ace your interviews at AI-first and AI-powered companies. Learn to explain how Gen AI really works, where it fails, and how to use it responsibly. Understand how top tech companies build and deploy AI products and how generative AI works in production, from LLMs and RAG systems to real-world deployment.