The "Single-Threaded CPU" interview question is a complex simulation problem that mimics how an operating system's scheduler might work. You are given a list of tasks, each with an enqueue time and a processing time. A single-threaded CPU can only process one task at a time. If the CPU is idle, it picks the task with the shortest processing time from the available pool. If two tasks have the same processing time, it picks the one with the smaller original index. This "Single-Threaded CPU coding problem" requires carefully managing the flow of time and the pool of available tasks.
Companies like Goldman Sachs and Microsoft ask this to evaluate a candidate's ability to handle complex state and multiple data structures simultaneously. It tests your proficiency with priority queues (heaps) and sorting. The problem requires you to think about event-driven simulation—how to efficiently jump between timestamps when the CPU is idle versus when it is busy.
This problem is a classic application of the "Sorting and Heap (Priority Queue) interview pattern".
(processing_time, original_index).Tasks: T0: (1, 2), T1: (2, 4), T2: (3, 2) (format: (enqueue, processing))
[(2, 0)].1 + 2 = 3.[(4, 1), (2, 2)].3 + 2 = 5.5 + 4 = 9.
Order: [0, 2, 1].One major pitfall is not handling the "idle CPU" case correctly, where no tasks are available in the heap but more tasks will arrive later. Another common mistake is failing to include the original index in the heap, which is necessary to break ties. Candidates also sometimes struggle with the logic of adding all eligible tasks to the heap before picking the next one to process.
Mastering the Priority Queue is essential for any "Single-Threaded CPU interview question". Remember that heaps are perfect for "find the minimum among currently available options" scenarios. Also, when sorting tasks that have multiple properties, always keep their original index if the problem requires it for output or tie-breaking.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Diagonal Traverse II | Medium | Solve | |
| Campus Bikes | Medium | Solve | |
| Choose K Elements With Maximum Sum | Medium | Solve | |
| Maximum Product of Two Elements in an Array | Easy | Solve | |
| Find the K-Sum of an Array | Hard | Solve |