Skip to content
DeepForgeby svx
All problems
HardData Structuresds-248

Task Priority Queue with Aging

Problem

Schedule tasks by priority with aging to prevent starvation.

tasks is a list of [name, priority, work], where a lower priority number is more urgent. Each tick the runnable task with the smallest effective priority (priority minus aging times its waiting ticks, with ties broken by name) executes one unit; other tasks age by one tick and the executed task's wait resets.

Return the completion order of task names.

Hint

Aging lowers the effective priority number of waiting tasks, letting old ones catch up.

Test cases

case 1
in: [[["a",5,2],["b",1,2]],1]
expected: ["b","a"]
case 2
in: [[["x",1,1],["y",1,3]],0]
expected: ["x","y"]
case 3
in: [[["p",2,1],["q",3,1]],5]
expected: ["p","q"]
case 4
in: [[],1]
expected: []
Starter codePython
def priority_aging(tasks, aging):
    # Your code here
    pass

4 test cases · run your code against them in the editor