Priority-Aware Dependent Task Order
Updated
Problem statement
You are given n = durations.length tasks numbered from 0 to n - 1. Task i takes durations[i] time units and has priority priorities[i]. Each row [before, after] in dependencies means task after may run only after task before finishes.
One worker executes exactly one task at a time. Whenever the worker becomes free, choose among all ready unfinished tasks using these rules:
- Choose the task with the greater numeric priority.
- If priorities tie, choose the smaller task ID.
Start at time 0 and never leave the worker idle while a task is ready. Return the complete schedule as rows [taskId, startTime, endTime] in execution order. The final row's end time is the minimum time needed by this one-worker policy to finish every task.
Interview extension
If tasks arrive over time, keep the same indegree counts and ready-task heap. Add each arriving task and its dependency edges to the maintained graph, and insert it into the heap as soon as every known prerequisite has finished.
Function
schedulePriorityTasks(durations: int[], priorities: int[], dependencies: int[][]) → int[][]Examples
Example 1
durations = [3,2,4,1,2]priorities = [2,5,1,5,3]dependencies = [[0,2],[1,2],[2,4]]return = [[1,0,2],[3,2,3],[0,3,6],[2,6,10],[4,10,12]]Tasks 1 and 3 initially tie for the greatest priority, so task 1 wins by smaller ID. Task 2 becomes ready only after both tasks 0 and 1 finish, and task 4 becomes ready after task 2.
Example 2
durations = [2,1,3]priorities = [1,9,5]dependencies = [[0,1],[1,2]]return = [[0,0,2],[1,2,3],[2,3,6]]The dependency chain determines the only valid order even though task 1 has the greatest priority.
Example 3
durations = [1,1,1]priorities = [2,2,3]dependencies = []return = [[2,0,1],[0,1,2],[1,2,3]]All tasks are ready at time 0. Task 2 has the greatest priority, then tasks 0 and 1 follow in ID order.
Constraints
1 <= n == durations.length == priorities.length <= 100000.1 <= durations[i] <= 1000.-10^9 <= priorities[i] <= 10^9.0 <= dependencies.length <= 200000.- Every dependency is a unique pair of distinct task IDs in
[0,n). - The dependency graph is acyclic.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Dependency-Aware Agent Task SchedulerOpenAI · PHONE SCREENOpenAI interview questions
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Lexicographically Smallest Course OrderLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- ETL Pipeline SchedulerUnity · ONSITE INTERVIEW
- Microservice Deployment OrderGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Course Schedule IIWalmart · ONSITE INTERVIEWWalmart interview questions
- Task Order With PrerequisitesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Course Schedule IIBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions