Batching and serving algorithms · Medium

Deficit Round-Robin Scheduling

Problem 44 of 50 · Python and NumPy

Problem

Simulate a bounded deficit round-robin scheduler. queues[i] is a FIFO list of positive token costs. All deficits start at zero. For each round, visit queue indices in ascending order. If a queue is nonempty, add quantum to its deficit, then serve as many head items as fit, subtracting their costs. Never skip an oversized head to serve a later item.

Whenever a queue is empty, reset its deficit to zero. Return {"served": [queue_index,cost] pairs in service order, "remaining": final queue lists, "deficits": final credits}. This trace model has no new arrivals.

def deficit_round_robin(queues, quantum, rounds):

Constraints

  • 0..8 queues, each with 0..16 positive integer costs ≤32.
  • 1 ≤ quantum ≤32; 0 ≤ rounds ≤32.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      1,
      1
    ],
    [
      1,
      1
    ]
  ],
  1,
  1
]
Expected result: {
  "served": [
    [
      0,
      1
    ],
    [
      1,
      1
    ]
  ],
  "remaining": [
    [
      1
    ],
    [
      1
    ]
  ],
  "deficits": [
    0,
    0
  ]
}

Each queue receives one credit and serves its first cost-1 item; one item remains per queue.

Example 2

Arguments: [
  [
    [
      3
    ]
  ],
  2,
  2
]
Expected result: {
  "served": [
    [
      0,
      3
    ]
  ],
  "remaining": [
    []
  ],
  "deficits": [
    0
  ]
}

Round 1 saves two credits. Round 2 reaches four, serves cost 3, and resets the now-empty queue’s leftover credit.

Sign in to solve this problem

Browse all coding problems · Learn the foundations