Named Async Task Scheduler
Updated
Problem statement
Simulate a scheduler that runs tasks asynchronously, so several tasks can be running at once. Process operations in order, and keep a virtual clock that starts at 0.
Operations
["ADD", name, taskId, duration]: add a task. Its duration is a positive integer. If no task with that name is running, start it now. Otherwise, put it at the back of that name's FIFO queue. FIFO means first in, first out: queued tasks leave in the order they joined.["ADVANCE", delta]: move the clock forward by delta, a nonnegative integer. The new time is the target time. Handle every task that finishes at or before the target time.["CANCEL", name]: cancel the task with that name that is running now. If there is one, start the next queued task with the same name right away. The rest of the queue stays.["CLEAR", name]: remove the running task and every queued task with that name. Tasks with other names are never affected.
Two tasks with the same name never run at the same time. Tasks with different names may run at the same time.
Completion ordering
A queued task starts at the exact time the task before it finishes or is cancelled. During ADVANCE, handle finishing tasks by earlier finish time first. If two tasks finish at the same time, handle first the one whose name is lexicographically smaller (string order: compare the names character by character using character codes, and a name that is the beginning of a longer name comes first). For each finishing task, record its FINISH event, then start the next task with that name before you handle the next finishing task. Durations are positive, so a task that just started cannot finish at that same moment.
After handling those tasks, set the clock to the target time. A task that finishes exactly at the target time completes before the next operation. When the operations run out, stop: tasks that have not finished are not run to the end.
Return value
Return every task event, in the order the events happen, using exactly these forms:
time:START:name:taskIdtime:QUEUE:name:taskIdtime:FINISH:name:taskIdtime:CANCEL:name:taskId, ortime:CANCEL_NONE:nametime:CLEAR:name:taskIdfor the running task first and then queued tasks in FIFO order, ortime:CLEAR_NONE:name
Function
runNamedTaskScheduler(operations: String[][]) → String[]Examples
Example 1
operations = [["ADD","alpha","a1","5"],["ADD","alpha","a2","2"],["ADD","beta","b1","3"],["ADVANCE","3"],["ADVANCE","2"],["ADVANCE","2"]]return = ["0:START:alpha:a1","0:QUEUE:alpha:a2","0:START:beta:b1","3:FINISH:beta:b1","5:FINISH:alpha:a1","5:START:alpha:a2","7:FINISH:alpha:a2"]The names alpha and beta are different, so a1 and b1 run at the same time. The second alpha task, a2, waits in the queue until a1 finishes. Meanwhile, b1 finishes on its own at time 3.
Example 2
operations = [["ADD","sync","s1","10"],["ADD","sync","s2","4"],["ADD","sync","s3","1"],["ADVANCE","3"],["CANCEL","sync"],["ADVANCE","4"],["ADVANCE","1"]]return = ["0:START:sync:s1","0:QUEUE:sync:s2","0:QUEUE:sync:s3","3:CANCEL:sync:s1","3:START:sync:s2","7:FINISH:sync:s2","7:START:sync:s3","8:FINISH:sync:s3"]Cancelling s1 at time 3 starts the next queued sync task, s2, right away. Task s1 would have finished at time 10, but it was cancelled, so no FINISH event for it ever appears.
Example 3
operations = [["ADD","red","r1","8"],["ADD","red","r2","2"],["ADD","blue","b1","4"],["ADVANCE","2"],["CLEAR","red"],["ADVANCE","2"],["CANCEL","red"],["CLEAR","red"]]return = ["0:START:red:r1","0:QUEUE:red:r2","0:START:blue:b1","2:CLEAR:red:r1","2:CLEAR:red:r2","4:FINISH:blue:b1","4:CANCEL_NONE:red","4:CLEAR_NONE:red"]Clearing red removes its running task r1 first and then its queued task r2. The blue task keeps running and finishes later. After that, no red task is left, so the cancel and the second clear give CANCEL_NONE and CLEAR_NONE.
Constraints
0 <= operations.length <= 100000.- Every row has one of the four exact forms above.
nameandtaskIdcontain 1 to 20 lowercase ASCII letters, digits, or underscores, and do not contain colons.- Every
taskIdin anADDoperation is globally unique. 1 <= duration <= 10^9and0 <= delta <= 10^9.- The virtual clock never exceeds
10^14. - The returned array contains at most
200000events.
More Airwallex interview questions
Similar questions at other companies
- Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Keyed Bounded Executor ScheduleMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Ad Score Scheduler With DelayGoogle · ONSITE INTERVIEWGoogle interview questions
- Progressive Banking SystemAirbnb · OAAirbnb interview questions
- Split Tender Part 4 - RefundsStripe · OA · PHONE SCREENStripe interview questions
- Cloud Compute Credits SystemCoinbase · OACoinbase interview questions
- Multi-Level Inventory Storage SystemOptiver · OA · PHONE SCREENOptiver interview questions
- Weighted LFU CacheIMC Trading · OAIMC Trading interview questions
- Get Priorities after Execution 🥭Amazon · OA
- Organization Reputation After Employee DeparturesHSBC · OAHSBC interview questions