Single-Threaded CPU
Learn this problemProblem statement
Source note, June 26, 2026: Please ignore this question for now, as it may have source issues. If you still want to practice it, treat it as a rough draft rather than a reliable source-backed problem. I will investigate and clean it up later, but this may take a while. Sorry for the inconvenience, and thank you so much for your understanding. You are the best! 🦭
You are given n tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTime_i, processingTime_i] means that the i-th task becomes available to process at enqueueTime_i and takes processingTime_i to finish.
You have a single-threaded CPU that can process at most one task at a time and acts as follows:
- If the CPU is idle and there are no available tasks to process, the CPU remains idle.
- If the CPU is idle and there are available tasks, the CPU chooses the one with the shortest processing time. If multiple available tasks have the same shortest processing time, it chooses the task with the smallest index.
- Once a task is started, the CPU processes the entire task without stopping.
- The CPU can finish a task and then start a new one instantly.
Return the order in which the CPU processes the tasks.
The standard approach sorts the tasks by enqueue time, advances a current time, and keeps a min-heap of currently-available tasks keyed by (processingTime, originalIndex). When the heap is empty but tasks remain, jump the clock forward to the next enqueue time.
Function
getOrder(tasks: int[][]) → int[]Examples
Example 1
tasks = [[1, 2], [2, 4], [3, 2], [4, 1]]return = [0, 2, 3, 1]Example 2
tasks = [[7, 10], [7, 12], [7, 5], [7, 4], [7, 2]]return = [4, 3, 2, 0, 1]Constraints
1 <= tasks.length <= 10^5tasks[i].length == 21 <= enqueueTime_i, processingTime_i <= 10^9
More Roblox problems
- Most Frequent Call Stack Per ThreadPHONE SCREEN · Seen Jul 2026
- Maximum Number of Balls in a BoxPHONE SCREEN · Seen Jun 2026
- Meeting Rooms IIPHONE SCREEN · Seen Jun 2026
- Most Frequent Call Path From Function Trace LogsPHONE SCREEN · Seen Jun 2026
- Sliding Window: Target Containment and Most-Repeated WindowPHONE SCREEN · Seen Jun 2026
- Topological Sort with Secondary OrderingPHONE SCREEN · Seen Jun 2026
- Merge IntervalsPHONE SCREEN · Seen May 2026
- Implement a Rate LimiterPHONE SCREEN · Seen May 2026