Single-Threaded CPU - Problem
Single-Threaded CPU Task Scheduler

Imagine you're managing a single-core CPU that needs to efficiently process multiple tasks. You have n tasks labeled from 0 to n - 1, where each task is represented by tasks[i] = [enqueueTime_i, processingTime_i].

The CPU follows these intelligent scheduling rules:
Idle State: If no tasks are available, the CPU waits
Task Selection: Among available tasks, pick the one with shortest processing time
Tie-Breaking: If processing times are equal, choose the task with smallest original index
Non-Preemptive: Once started, a task runs to completion
Instant Switching: CPU can immediately start the next task

Goal: Return the order in which the CPU processes all tasks, demonstrating optimal task scheduling in action!

Input & Output

example_1.py — Basic Task Scheduling
$ Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
Output: [0,2,3,1]
💡 Note: At t=1, only task 0 is available (process for 2 units until t=3). At t=3, tasks 1 and 2 are available; choose task 2 (shorter processing time, process until t=5). At t=5, tasks 1 and 3 are available; choose task 3 (shorter processing time, process until t=6). Finally process task 1 from t=6 to t=10.
example_2.py — Tie-Breaking by Index
$ Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
Output: [4,3,2,0,1]
💡 Note: All tasks become available at t=7. Since they all have the same enqueue time, we process them in order of processing time: task 4 (2 units), task 3 (4 units), task 2 (5 units), task 0 (10 units), task 1 (12 units).
example_3.py — CPU Idle Time
$ Input: tasks = [[5,2],[7,2]]
Output: [0,1]
💡 Note: At t=0, no tasks are available so CPU is idle until t=5. Process task 0 from t=5 to t=7. At t=7, task 1 becomes available and is processed from t=7 to t=9.

Constraints

  • 1 ≤ tasks.length ≤ 105
  • 1 ≤ enqueueTimei, processingTimei ≤ 109
  • All task indices are unique and range from 0 to n-1

Visualization

Tap to expand
🖥️ Single-Threaded CPU Task SchedulerTimeline VisualizationT0T2T3T1t=1t=3t=5t=6t=10📋 Task Queue (Sorted)Task 0: [1, 2] ← NextTask 1: [2, 4]Task 2: [3, 2]Task 3: [4, 1]🔄 Min-Heap (Available)(2,0)(2,2)(4,1)Min by processing time✅ Processing Order0→ First (t=1-3)2→ Second (t=3-5)3→ Third (t=5-6)1→ Last (t=6-10)⚡ Algorithm EfficiencySorting: O(n log n) - Organize tasks chronologicallyHeap Operations: O(n log n) - Each task pushed/popped onceTotal Time: O(n log n) - Optimal for this problemSpace: O(n) - Heap stores up to n tasks🎯 Key Insight: Priority Queue + Chronological ProcessingEfficiently manages task availability and selection using heap data structure
Understanding the Visualization
1
Sort by Arrival
Arrange all tasks chronologically by their enqueue time
2
Build Available Pool
Add all tasks that have arrived by current time to the priority queue
3
Select Optimal Task
Pick task with shortest processing time (break ties by original index)
4
Execute and Advance
Process the selected task and advance CPU time by processing duration
5
Repeat Until Complete
Continue until all tasks are processed, handling CPU idle time efficiently
Key Takeaway
🎯 Key Insight: The optimal solution combines chronological task processing (sorting) with efficient priority management (min-heap) to achieve O(n log n) time complexity while handling CPU idle time intelligently.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 25
52.0K Views
High Frequency
~25 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen