Simulate a single-threaded CPU that picks the shortest task, breaking ties by priority then index

Quick Overview

Given tasks with arrival times, processing times and priorities, return the order in which a non-preemptive single-threaded CPU runs them when it always takes the shortest available task, breaking ties by priority and then by index. It tests event-driven simulation with a priority queue and exact tie-breaking.

Simulate a single-threaded CPU that picks the shortest task, breaking ties by priority then index

Company: Asana

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given `n` tasks labeled from `0` to `n - 1` in a 2D integer array `tasks`, where `tasks[i] = [enqueue_time, processing_time, priority]` means that task `i` becomes available to process at `enqueue_time`, takes `processing_time` to finish, and has priority `priority`. A lower `priority` value means a higher priority. You have a single-threaded CPU that can process at most one task at a time and acts in the following way: - 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 tasks share the shortest processing time, it chooses the one with the highest priority (smallest `priority` value). If there is still a tie, 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 start a new one instantly. Return the order in which the CPU processes the tasks. ### Function Signature ```python def get_order(tasks: list[list[int]]) -> list[int]: ``` ### Rules - Each `tasks[i]` is `[enqueue_time, processing_time, priority]`, and a task's index is its 0-based position in the input list. - A task is available at every time `t >= enqueue_time`. A task whose enqueue time equals the moment the CPU finishes its current task is already available for the next choice. - Among the available tasks, the CPU picks the one with the smallest `(processing_time, priority, index)`, compared in that order. This makes the output unique. - The CPU never preempts. A task that arrives while another task is running waits until that run finishes. - Return all `n` task indices in the order the CPU starts them. ### Constraints - `1 <= len(tasks) <= 10^4` - `len(tasks[i]) == 3` - `1 <= enqueue_time, processing_time <= 10^9` - `1 <= priority <= 10^9` ### Examples ```text Input: tasks = [[1,2,3],[2,4,1],[3,2,2],[4,1,5]] Output: [0,2,3,1] ``` At time 1 only task 0 is available, so it runs until time 3. Then tasks 1 and 2 are waiting and task 2 is shorter, so it runs until time 5, when task 3 (processing time 1) beats task 1, and task 1 runs last. ```text Input: tasks = [[7,10,2],[7,12,1],[7,5,3],[7,4,4],[7,2,5]] Output: [4,3,2,0,1] ``` All five tasks arrive at time 7 with different processing times, so the CPU runs them shortest first (2, 4, 5, 10, 12). Priority never decides anything here. ```text Input: tasks = [[1,3,2],[1,3,1],[2,3,1],[2,1,9],[3,3,1]] Output: [1,3,2,4,0] ``` At time 1, tasks 0 and 1 tie on processing time 3 and task 1 wins on priority (1 < 2), running until time 4. By then tasks 0, 2, 3 and 4 are waiting: task 3 is shortest and goes first even with priority 9, then tasks 2 and 4 (same time and priority, so the smaller index first), and task 0 last.

Overview: Given tasks with arrival times, processing times and priorities, return the order in which a non-preemptive single-threaded CPU runs them when it always takes the shortest available task, breaking ties by priority and then by index. It tests event-driven simulation with a priority queue and exact tie-breaking.

Asana logo
Asana
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given n tasks labeled from 0 to n - 1 in a 2D integer array tasks, where tasks[i] = [enqueue_time, processing_time, priority] means that task i becomes available to process at enqueue_time, takes processing_time to finish, and has priority priority. A lower priority value means a higher priority.

You have a single-threaded CPU that can process at most one task at a time and acts in the following way:

  • 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 tasks share the shortest processing time, it chooses the one with the highest priority (smallest priority value). If there is still a tie, 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 start a new one instantly.

Return the order in which the CPU processes the tasks.

Function Signature

def get_order(tasks: list[list[int]]) -> list[int]:

Rules

  • Each tasks[i] is [enqueue_time, processing_time, priority] , and a task's index is its 0-based position in the input list.
  • A task is available at every time t >= enqueue_time . A task whose enqueue time equals the moment the CPU finishes its current task is already available for the next choice.
  • Among the available tasks, the CPU picks the one with the smallest (processing_time, priority, index) , compared in that order. This makes the output unique.
  • The CPU never preempts. A task that arrives while another task is running waits until that run finishes.
  • Return all n task indices in the order the CPU starts them.

Constraints

  • 1 <= len(tasks) <= 10^4
  • len(tasks[i]) == 3
  • 1 <= enqueue_time, processing_time <= 10^9
  • 1 <= priority <= 10^9

Examples

Input: tasks = [[1,2,3],[2,4,1],[3,2,2],[4,1,5]]
Output: [0,2,3,1]

At time 1 only task 0 is available, so it runs until time 3. Then tasks 1 and 2 are waiting and task 2 is shorter, so it runs until time 5, when task 3 (processing time 1) beats task 1, and task 1 runs last.

Input: tasks = [[7,10,2],[7,12,1],[7,5,3],[7,4,4],[7,2,5]]
Output: [4,3,2,0,1]

All five tasks arrive at time 7 with different processing times, so the CPU runs them shortest first (2, 4, 5, 10, 12). Priority never decides anything here.

Input: tasks = [[1,3,2],[1,3,1],[2,3,1],[2,1,9],[3,3,1]]
Output: [1,3,2,4,0]

At time 1, tasks 0 and 1 tie on processing time 3 and task 1 wins on priority (1 < 2), running until time 4. By then tasks 0, 2, 3 and 4 are waiting: task 3 is shortest and goes first even with priority 9, then tasks 2 and 4 (same time and priority, so the smaller index first), and task 0 last.