Single-Threaded CPU
Updated
Problem statement
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]The CPU starts at t=1 with task 0 (length 2). At t=3, tasks 1 and 2 are ready, and task 2 wins (length 2 vs 4). At t=5, task 3 has arrived and runs next (length 1), because it is shorter than the task still waiting. At t=6 task 1 finally runs (length 4), which completes the order.
Example 2
tasks = [[7, 10], [7, 12], [7, 5], [7, 4], [7, 2]]return = [4, 3, 2, 0, 1]All tasks arrive at t=7. The CPU drains them by shortest processing time, breaking ties by smallest index: task 4 (2), task 3 (4), task 2 (5), task 0 (10), task 1 (12).
Constraints
1 <= tasks.length <= 10^5tasks[i].length == 21 <= enqueueTime_i, processingTime_i <= 10^9
More Roblox Phone Screen questions
- Design Search Autocomplete SystemPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Most Frequent Call Path From Function Trace LogsPHONE SCREEN · Seen Nov 2026
- Candy Crush Grid Matching and GravityPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Implement a Rate LimiterPHONE SCREEN · Seen Oct 2026
- Rate Limit by Multiple Request FieldsPHONE SCREEN · Seen Oct 2026
- Topological Sort with Secondary OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Time-Aware Search AutocompletePHONE SCREEN · Seen Oct 2026
- Most Frequent Call Stack Per ThreadPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Rate Limiter Sliding Window With Per-Entity LimitsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Closest Binary Search Tree Value — Base PracticePHONE SCREEN · Seen Jun 2026
Similar questions at other companies
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Meeting Rooms IIeBay · PHONE SCREENeBay interview questions
- Select the Top User MatchesDuolingo · PHONE SCREENDuolingo interview questions
- Meeting Rooms IIQuince · PHONE SCREENQuince interview questions
- Kth Largest Element in an ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Meeting Rooms IISecuronix · PHONE SCREEN
- Merge Multiple Sorted StreamsNVIDIA · PHONE SCREENNVIDIA interview questions
Roblox interview experiences
- Roblox OA: What It Tests (2026)
- Roblox New Grad Early OA Tasks: 10 Experiences (Part 7 of 7)
- Roblox New Grad Engineer Rounds: 13 Experiences (Part 6 of 7)
- Roblox New Grad HackerRank OA: 18 Experiences (Part 5 of 7)
- Roblox New Grad CodeSignal Finals: 23 Experiences (Part 4 of 7)
- Roblox New Grad Games and BQ: 25 Experiences (Part 3 of 7)