1834. Single-Threaded CPU
Asked at TikTok
Difficulty
Medium
Time
O(n log n)
Space
O(n)
Pattern
Array
Companies
1
Problem
You are given n tasks, numbered 0 to n-1. Each task is a pair tasks[i] = [enqueueTime, processingTime]: the task becomes available at enqueueTime, and it needs processingTime units of work to finish.
There is one CPU, and it can run only one task at a time. When the CPU is free, it looks at all tasks that have already become available and picks the one with the smallest processing time. If two tasks have the same processing time, it picks the one with the smaller index. If no task is available yet, the CPU does nothing until the next task appears. Once a task starts, it runs to completion without interruption, and the CPU can start the next task at the exact moment the previous one ends.
Return the list of task indices in the order the CPU processes them. The answer has length n and contains every index exactly once.
Who runs next?
Constraints
- ●
1 <= tasks.length <= 10^5 - ●
tasks[i] = [enqueueTime_i, processingTime_i] - ●
1 <= enqueueTime_i, processingTime_i <= 10^9
Examples
tasks = [[1,2],[2,4],[3,2],[4,1]][0,2,3,1]tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]][4,3,2,0,1]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| TikTok | Medium | View all TikTok questions → |
How to Think About It
Two kinds of events matter: tasks becoming available (enqueue times) and the CPU finishing a task.
Sort task indices by enqueue time. Keep a pointer into that sorted list and a min-heap of available tasks keyed by (processingTime, index).
Loop: push every task whose enqueue time is <= time. If the heap is empty, jump time forward to the next enqueue time. Otherwise pop the best task, record it, and advance time by its processing time.
The time jump when idle is important — it avoids stepping one unit at a time across long gaps.
Use 64-bit time: processing times up to 10^9 for 10^5 tasks can exceed 32 bits.
Optimal Approach
Step 1: order = indices sorted by enqueueTime.
Step 2: time = 0, i = 0, heap = [], res = [].
Step 3: While len(res) < n:
If heap is empty and time < enqueue[order[i]]: time = enqueue[order[i]].
Push all tasks with enqueue <= time as (proc, idx).
Pop (p, idx), append idx, time += p.
Step 4: Return res.
Time: O(n log n). Space: O(n).
Sort, then simulate with a min-heap
What Trips People Up in Real Interviews
Sorting the tasks and losing their original indices. Sort an index array instead.
Incrementing time by 1 while idle, which is far too slow for large enqueue times.
Tie-breaking by enqueue time instead of index. With equal processing times the smaller index wins.
32-bit overflow on the running time in C++/Java.
Solution Code
O(n log n) timeimport heapq
def getOrder(tasks):
n = len(tasks)
order = sorted(range(n), key=lambda i: tasks[i][0])
heap = []
res = []
time = 0
i = 0
while len(res) < n:
if not heap and time < tasks[order[i]][0]:
time = tasks[order[i]][0]
while i < n and tasks[order[i]][0] <= time:
idx = order[i]
heapq.heappush(heap, (tasks[idx][1], idx))
i += 1
p, idx = heapq.heappop(heap)
res.append(idx)
time += p
return resFrequently Asked Questions
What is the Single-Threaded CPU problem?
Single-Threaded CPU gives you tasks with an enqueue time and a processing time. A single CPU, whenever idle, picks the available task with the shortest processing time (ties broken by index); if nothing is available, it waits for the next task. Return the order tasks are processed. It is an event simulation driven by a sorted list and a min-heap.
How do you solve Single-Threaded CPU?
The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Single-Threaded CPU?
Single-Threaded CPU is asked at TikTok. It is a medium difficulty problem.
What are common mistakes on Single-Threaded CPU?
- Sorting the tasks and losing their original indices. Sort an index array instead.
- Incrementing time by 1 while idle, which is far too slow for large enqueue times.
- Tie-breaking by enqueue time instead of index. With equal processing times the smaller index wins.
- 32-bit overflow on the running time in C++/Java.