Simulate a Nonpreemptive CPU with Deterministic Ties

Quick Overview

Simulate a Nonpreemptive CPU with Deterministic Ties. Task i has enqueue and processing times in tasks[i]. When idle, the CPU chooses an available task with the shortest processing time, breaking ties by smaller original index.

Simulate a Nonpreemptive CPU with Deterministic Ties

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Task i has enqueue and processing times in `tasks[i]`. When idle, the CPU chooses an available task with the shortest processing time, breaking ties by smaller original index. It runs the selected task to completion and may start the next instantly. If none is available, it waits. Return the original task indices in processing order. ```cpp class Solution { public: vector<int> getOrder(vector<vector<int>>& tasks); }; ``` ### Examples **Example 1:** ```text tasks = [[2,3],[2,1],[3,1],[9,2]] Output: [1,2,0,3] Explanation: At time 2 choose task 1; at 3 choose task 2; then task 0 runs until 7, and the CPU waits until 9. ``` **Example 2:** ```text tasks = [[1,2],[1,2],[2,1]] Output: [0,2,1] Explanation: Equal processing times initially choose index 0; task 2 is available when it finishes. ``` ### Constraints - `1 <= tasks.length <= 10^5`. - Each task has two integers: enqueue time and processing time, both in `[1,10^9]`. ### Clarifying Questions Execution is not preempted by arrivals. All tasks enqueued at or before the completion time are eligible for the next selection. ```hint Inspect the structure Which tasks are eligible when the CPU makes a choice, and how are equal processing times resolved? ``` ### Reasoning checkpoints - Respect release times, nonpreemption, and both levels of the task-selection rule. - Preserve original task indices and use arithmetic valid for accumulated execution times. ### Optional extensions What changes if a newly arrived shorter job may preempt the currently running task?

Overview: Simulate a Nonpreemptive CPU with Deterministic Ties. Task i has enqueue and processing times in tasks[i]. When idle, the CPU chooses an available task with the shortest processing time, breaking ties by smaller original index.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Task i has enqueue and processing times in tasks[i]. When idle, the CPU chooses an available task with the shortest processing time, breaking ties by smaller original index. It runs the selected task to completion and may start the next instantly. If none is available, it waits. Return the original task indices in processing order.

class Solution {
public:
    vector<int> getOrder(vector<vector<int>>& tasks);
};

Examples

Example 1:

tasks = [[2,3],[2,1],[3,1],[9,2]]
Output: [1,2,0,3]
Explanation: At time 2 choose task 1; at 3 choose task 2; then task 0 runs until 7, and the CPU waits until 9.

Example 2:

tasks = [[1,2],[1,2],[2,1]]
Output: [0,2,1]
Explanation: Equal processing times initially choose index 0; task 2 is available when it finishes.

Constraints

  • 1 <= tasks.length <= 10^5 .
  • Each task has two integers: enqueue time and processing time, both in [1,10^9] .

Clarifying Questions Guidance

Execution is not preempted by arrivals. All tasks enqueued at or before the completion time are eligible for the next selection.

Reasoning checkpoints

  • Respect release times, nonpreemption, and both levels of the task-selection rule.
  • Preserve original task indices and use arithmetic valid for accumulated execution times.

Optional extensions

What changes if a newly arrived shorter job may preempt the currently running task?