PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/xAI

Minimum Time to Run All Jobs with a Cooldown Between Identical Jobs

Last updated: Jul 2, 2026

Minimum Time to Run All Jobs with a Cooldown Between Identical Jobs

Company: xAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given a list of jobs to run on a single-core CPU, where each job is represented by an uppercase letter `'A'`–`'Z'` in an array `jobs`. Jobs of the same letter are identical instances of the same job type and each instance takes exactly **one time unit** to run. You are also given a non-negative integer `cooldown`. Between two runs of the **same job type**, there must be at least `cooldown` time units in which the CPU is doing something else — either running a different job type or sitting **idle**. In each time unit the CPU either runs exactly one job instance or stays idle. Jobs may be executed in **any order**. Return the **minimum number of time units** the CPU needs to finish all jobs in `jobs`. **Example 1:** ``` Input: jobs = ["A","A","A","B","B","B"], cooldown = 2 Output: 8 Explanation: One optimal schedule is A -> B -> idle -> A -> B -> idle -> A -> B Each pair of consecutive A's (and B's) is separated by 2 time units. ``` **Example 2:** ``` Input: jobs = ["A","A","A","B","B","B"], cooldown = 0 Output: 6 Explanation: With no cooldown, the jobs can run back to back in any order, e.g. A -> A -> A -> B -> B -> B. ``` **Example 3:** ``` Input: jobs = ["A","A","A","A","A","A","B","C","D","E","F","G"], cooldown = 2 Output: 16 Explanation: One optimal schedule is A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A ``` **Constraints:** - `1 <= jobs.length <= 10^4` - `jobs[i]` is an uppercase English letter `'A'`–`'Z'` - `0 <= cooldown <= 100` Aim for a solution that runs in `O(n)` time, where `n` is the number of job instances.

Related Interview Questions

  • Flatten and unflatten nested Python structures - xAI (nan)
  • Compute dasher pay from order events - xAI (nan)
  • Compute total active time per Twitter Space - xAI (medium)
  • Design a Recoverable Iterator - xAI (medium)
  • Implement Distributed Matrix Multiplication - xAI (hard)
|Home/Coding & Algorithms/xAI

Minimum Time to Run All Jobs with a Cooldown Between Identical Jobs

xAI logo
xAI
Jul 30, 2025, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

You are given a list of jobs to run on a single-core CPU, where each job is represented by an uppercase letter 'A'–'Z' in an array jobs. Jobs of the same letter are identical instances of the same job type and each instance takes exactly one time unit to run.

You are also given a non-negative integer cooldown. Between two runs of the same job type, there must be at least cooldown time units in which the CPU is doing something else — either running a different job type or sitting idle.

In each time unit the CPU either runs exactly one job instance or stays idle. Jobs may be executed in any order.

Return the minimum number of time units the CPU needs to finish all jobs in jobs.

Example 1:

Input:  jobs = ["A","A","A","B","B","B"], cooldown = 2
Output: 8
Explanation: One optimal schedule is
A -> B -> idle -> A -> B -> idle -> A -> B
Each pair of consecutive A's (and B's) is separated by 2 time units.

Example 2:

Input:  jobs = ["A","A","A","B","B","B"], cooldown = 0
Output: 6
Explanation: With no cooldown, the jobs can run back to back in any
order, e.g. A -> A -> A -> B -> B -> B.

Example 3:

Input:  jobs = ["A","A","A","A","A","A","B","C","D","E","F","G"], cooldown = 2
Output: 16
Explanation: One optimal schedule is
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A

Constraints:

  • 1 <= jobs.length <= 10^4
  • jobs[i] is an uppercase English letter 'A' – 'Z'
  • 0 <= cooldown <= 100

Aim for a solution that runs in O(n) time, where n is the number of job instances.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More xAI•More Software Engineer•xAI Software Engineer•xAI Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.