Draft

Timed queue

Description:

You will be given an array of Tasks and queue size. Tasks go to queue to be done.

Task has the following interface:

interface Task {
  value: number,
  timeout: number,
}

Fulfilling a Task means to take its value and put it to the resulting array after the task's timeout. The Task might be done only if it is taken to the queue (i.e. time passes only for queued tasks).

(!) There should be no more than size tasks at the same time in the queue.

When the task is done, the next task takes his place in the queue (without delay). In case of timing parity, return the values according to the order in the queue.

Return the resulting array.

EXAMPLE

For input parameters:

tasks = [
  {value: 1, timeout: 50 }, 
  {value: 2, timeout: 100 }, 
  {value: 3, timeout: 70 }, 
  {value: 4, timeout: 10 }, 
  {value: 5, timeout: 200 }
];
size = 3;

The process would be:

  1. 3 first tasks are taken to queue.
  2. Task with value of 1 will be done first because it requires at least time units (50). After this task, task with value 4 will take his place.
  3. Task with value 4 requires only 10 time units and it is still less then other tasks in queue need (100 - 50 = 50 for the second Task, and 70 - 50 = 20 for the third). So it will be finished next and task with value 5 will take his place.
  4. At this point, all Tasks were queued, so we wait until they are done.

The resulting array would be:

[1, 4, 3, 2, 5]

More By Author:

Check out these other kata created by mo.mo.

Stats:

CreatedFeb 9, 2024
Warriors Trained4
Total Skips0
Total Code Submissions4
Total Times Completed1
JavaScript Completions1
Total Stars0
% of votes with a positive feedback rating0% of 0
Total "Very Satisfied" Votes0
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Ad
Contributors
  • mo.mo. Avatar
Ad