Schedule Tasks by Deadline and Priority
Heaps, Sorting & SearchAuto-graded · 15 test casesLast updated
Tests the ability to sort a list of tasks using a multi-key custom comparator involving deadlines, priorities, and tie-breaking rules.
Schedule Tasks by Deadline and Priority
You are building a task scheduler. Each task is represented as a dictionary with the following fields:
| Field | Type | Description |
|---|---|---|
"id" | str | Unique task identifier |
"deadline" | int | The time unit by which the task must be completed (lower = sooner) |
"priority" | int | Urgency level (higher = more urgent) |
"duration" | int | How many time units the task takes to run |
Given a list of such task dictionaries, return a list of task id strings ordered by the following scheduling rules, applied in order:
- Earliest deadline first — tasks with a smaller
deadlinecome first. - Highest priority first — among tasks with equal deadlines, tasks with a larger
prioritycome first. - Shortest duration first — among tasks with equal deadline and priority, tasks with a smaller
durationcome first. - Lexicographically smallest id first — among tasks identical on all above fields, sort by
idin ascending lexicographic order.
Return only the list of id strings in the computed order.
Constraints
0 <= len(tasks) <= 2000- Each
deadlineis an integer with1 <= deadline <= 10^6 - Each
priorityis an integer with1 <= priority <= 10^9 - Each
durationis an integer with1 <= duration <= 10^4 - Each
idis a non-empty string of lowercase letters and digits; allidvalues are unique. - The result must be deterministic: all four tie-breaking rules ensure exactly one valid ordering.
Examples
Example 1
Input: schedule_tasks([{'id': 'alpha', 'deadline': 5, 'priority': 3, 'duration': 2}, {'id': 'beta', 'deadline': 3, 'priority': 5, 'duration': 4}, {'id': 'gamma', 'deadline': 5, 'priority': 3, 'duration': 1}])
Output: ['beta', 'gamma', 'alpha']
Example 2
Input: schedule_tasks([{'id': 'x1', 'deadline': 10, 'priority': 7, 'duration': 3}, {'id': 'x2', 'deadline': 10, 'priority': 7, 'duration': 3}, {'id': 'x3', 'deadline': 10, 'priority': 7, 'duration': 3}])
Output: ['x1', 'x2', 'x3']
Example 3
Input: schedule_tasks([])
Output: []
Hints
Hint 1
Think about what tuple to return from a key function so that Python's built-in sort handles all four criteria at once.
Hint 2
Remember that Python sorts tuples lexicographically — so the order of elements in your key tuple directly controls which criterion is applied first.
Hint 3
For 'highest priority first', you need to reverse that one dimension. Negating an integer value lets you flip a single field's sort direction without affecting the others.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is to map all four scheduling criteria onto a single sort key tuple. Python's sorted() compares tuples element-by-element, so the order of elements in the tuple directly encodes priority among criteria.
Key construction
For each task, produce:
(deadline, -priority, duration, id)
deadline— ascending naturally; smaller values sort first.-priority— negating an integer flips the direction: a higher original priority becomes a more-negative key value, which sorts earlier.duration— ascending naturally; shorter tasks sort first.id— ascending lexicographic order, Python's default for strings.
Because all four criteria together are guaranteed to produce a unique ordering (all id values are distinct, and id is the final tiebreaker), the result is fully deterministic.
Correctness
sorted() is stable but that property is irrelevant here since the four-field key is always unique. The construction is a direct one-to-one translation of the problem's rules into a tuple comparison.
Complexity
- Time: O(n log n) — dominated by the sort.
- Space: O(n) — for the output list and internal sort buffer.
def schedule_tasks(tasks: list) -> list:
def sort_key(task):
return (
task["deadline"], # 1. earliest deadline first (ascending)
-task["priority"], # 2. highest priority first (negate for descending)
task["duration"], # 3. shortest duration first (ascending)
task["id"], # 4. lexicographically smallest id first (ascending)
)
sorted_tasks = sorted(tasks, key=sort_key)
return [task["id"] for task in sorted_tasks]