Single-Threaded CPU
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
A CPU runs one task at a time. You are given tasks, where tasks[i] = [enqueueTime, processingTime] means task i becomes available at time enqueueTime and needs processingTime units to finish once started.
The CPU follows these rules, starting at time 0:
- If it is idle and no task is available, it waits until the next task becomes available.
- If it is idle and tasks are available, it starts the one with the **shortest processingTime**; ties go to the **smallest index**.
- A started task runs to completion without interruption. The CPU may pick its next task the instant the previous one ends, and tasks whose enqueueTime equals that instant count as available.
Return the task indices in the order the CPU processes them. Target O(n log n).
Examples
Constraints
- 1 <= tasks.length <= 10^5
- 1 <= enqueueTime, processingTime <= 10^9
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.