Calculate server loads using Round Robin Scheduling
Last Updated :
23 Jul, 2025
Given M servers that handle multiple requests having infinite computational capability and arrays arrivalTime[] and processTime[] of size N denoting the arrival time and load time of N requests in the following manner:
- Each server is numbered from 0 to (M - 1) and the requests are given in strictly increasing order of time.
- Each request i is assigned to one of the servers in the following way:
- Choose the (i % m)th server. If the chosen server is free, assign the request to the server.
- Otherwise, choose the next available server. If no server is available, then the request is dropped.
Considering that each server can handle only one request at a time, the task is to find the load on each server after all the incoming requests are processed given that load on each server is the number of requests it processes.
Examples:
Input: N = 4, M = 3, arrivalTime[] = {1, 3, 6, 8}, processTime[] = {1, 2, 2, 1}
Output:
1st Server -> 2
2nd Server -> 1
3rd Server -> 1
Explanation:
The first and fourth requests are assigned to the first server.
The second request is assigned to the second server and the third request is assigned to the third server.
Below is the transition table:
Request Number | Arrival Time | Load Time | End Time | Available Servers | Demanded Server | Assigned Server |
0 | 1 | 1 | 2 | 0, 1, 2 | 0 | 0 |
1 | 3 | 2 | 5 | 0, 1, 2 | 1 | 1 |
2 | 6 | 2 | 8 | 0, 1, 2 | 2 | 2 |
3 | 8 | 1 | 9 | 0, 1, 2 | 1 | 1 |
Input: N = 4, M = 2, arrivalTime = {1, 2, 4, 6}, processTime = {7, 1, 4, 4}
Output:
1st Server -> 1
2nd Server -> 2
Explanation:
The first request is assigned to the first server and second request to the second server.
The third request is assigned to the second server. The demanded server for the third request is the first server but since, it is busy at the arrival time of the request,
So, the second server is assigned to it.
The fourth request is dropped as both servers are busy at the time of its arrival.
Below is the transition table:
Request Number | Arrival Time | Load Time | End Time | Available Servers | Demanded Server | Assigned Server |
0 | 1 | 7 | 8 | 0, 1 | 0 | 0 |
1 | 2 | 1 | 3 | 1 | 1 | 1 |
2 | 4 | 4 | 8 | 1 | 0 | 1 |
3 | 6 | 4 | 10 | - | 1 | - |
Approach: The idea is to use a Minimum Priority Queue and a set. Priority queue keeps count of the busy servers and helps to release them as soon as they are free. Set is used to maintain the data of available servers to assign them to the incoming requests. Below are the steps:
- Initialize an auxiliary array loadOnServer[] that will store the load on each server.
- Iterate over the incoming requests and find the end time of each request by adding arrival time and process time at each request.
- Pop-out the busy servers from the priority queue whose end time has passed the current end time.
- If the set of available servers is empty, drop the current request.
- Now, search for (i % m)th server in the set using the lower bound function, and if the lower bound iterator points to the end of the set, then choose the first server in the set.
- Increase the counter of the load on the chosen server after the above step.
- After the above steps, print all the load's stores in loadOnServer[].
Below is the implementation of the above approach:
C++
// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print load on each server
void printLoadOnEachServer(
int m, int loadOnServer[])
{
// Traverse the loadOnServer and
// print each loads
for (int i = 0; i < m; i++) {
cout << i + 1 << "st Server -> "
<< loadOnServer[i] << ".\n";
}
}
// Function for finding the load
// on each server
void loadBalancing(int n, int m,
int arrivalTime[],
int processTime[])
{
// Stores the load on each Server
int loadOnServer[m];
for (int i = 0; i < m; i++) {
// Initialize load on each
// server as zero
loadOnServer[i] = 0;
}
// Minimum priority queue for
// storing busy servers according
// to their release time
priority_queue<pair<int, int>,
vector<pair<int, int> >,
greater<pair<int, int> > >
busyServers;
// Set to store available Servers
set<int> availableServers;
for (int i = 0; i < m; i++) {
// Initially, all servers are free
availableServers.insert(i);
}
// Iterating through the requests.
for (int i = 0; i < n; i++) {
// End time of current request
// is the sum of arrival time
// and process time
int endTime = arrivalTime[i]
+ processTime[i];
// Releasing all the servers which
// have become free by this time
while (!busyServers.empty()
&& busyServers.top().first
<= arrivalTime[i]) {
// Pop the server
pair<int, int> releasedServer
= busyServers.top();
busyServers.pop();
// Insert available server
availableServers.insert(
releasedServer.second);
}
// If there is no free server,
// the request is dropped
if ((int)availableServers.empty()) {
continue;
}
int demandedServer = i % m;
// Searching for demanded server
auto itr
= availableServers.lower_bound(
demandedServer);
if (itr == availableServers.end()) {
// If demanded Server is not free
// and no server is free after it,
// then choose first free server
itr = availableServers.begin();
}
int assignedServer = *itr;
// Increasing load on assigned Server
loadOnServer[assignedServer]++;
// Removing assigned server from list
// of assigned servers
availableServers.erase(assignedServer);
// Add assigned server in the list of
// busy servers with its release time
busyServers.push({ endTime,
assignedServer });
}
// Function to print load on each server
printLoadOnEachServer(m, loadOnServer);
}
// Driver Code
int main()
{
// Given arrivalTime and processTime
int arrivalTime[] = { 1, 2, 4, 6 };
int processTime[] = { 7, 1, 4, 4 };
int N = sizeof(arrivalTime)
/ sizeof(int);
int M = 2;
// Function Call
loadBalancing(N, M, arrivalTime,
processTime);
return 0;
}
Java
import java.util.*;
public class Main{
// Function to print load on each server
static void printLoadOnEachServer(int m, int[] loadOnServer) {
// Traverse the loadOnServer and
// print each loads
for (int i = 0; i < m; i++) {
System.out.println((i + 1) + "st Server -> " + loadOnServer[i] + ".");
}
}
// Function for finding the load
// on each server
static void loadBalancing(int n, int m, int[] arrivalTime, int[] processTime) {
// Stores the load on each Server
int[] loadOnServer = new int[m];
for (int i = 0; i < m; i++) {
// Initialize load on each
// server as zero
loadOnServer[i] = 0;
}
// Minimum priority queue for
// storing busy servers according
// to their release time
PriorityQueue<int[]> busyServers = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
// Set to store available Servers
TreeSet<Integer> availableServers = new TreeSet<>();
for (int i = 0; i < m; i++) {
// Initially, all servers are free
availableServers.add(i);
}
// Iterating through the requests.
for (int i = 0; i < n; i++) {
// End time of current request
// is the sum of arrival time
// and process time
int endTime = arrivalTime[i] + processTime[i];
// Releasing all the servers which
// have become free by this time
while (!busyServers.isEmpty() && busyServers.peek()[0] <= arrivalTime[i]) {
// Pop the server
int[] releasedServer = busyServers.poll();
// Insert available server
availableServers.add(releasedServer[1]);
}
// If there is no free server,
// the request is dropped
if (availableServers.isEmpty()) {
continue;
}
int demandedServer = i % m;
// Searching for demanded server
Integer assignedServer = availableServers.ceiling(demandedServer);
if (assignedServer == null) {
// If demanded Server is not free
// and no server is free after it,
// then choose first free server
assignedServer = availableServers.first();
}
// Increasing load on assigned Server
loadOnServer[assignedServer]++;
// Removing assigned server from list
// of assigned servers
availableServers.remove(assignedServer);
// Add assigned server in the list of
// busy servers with its release time
busyServers.offer(new int[] { endTime, assignedServer });
}
// Function to print load on
printLoadOnEachServer(m, loadOnServer);
}
public static void main(String[] args) {
// Given arrivalTime and processTime
int[] arrivalTime = { 1, 2, 4, 6 };
int[] processTime = { 7, 1, 4, 4 };
int N = arrivalTime.length;
int M = 2;
// Function Call
loadBalancing(N, M, arrivalTime, processTime);
}
}
JavaScript
<script>
// javascript Program for the above approach
// function to calculate lower bound
function lowerbound(arr,target)
{
let N = arr.length;
let low=0;
for(let i=0;i<N-1;i++)
{
if(arr[i]<target && arr[i+1]>=target)
{
low = i+1;
break;
}
}
if(arr[0]>target) return 0;
return low;
}
// Function to print load on each server
function printLoadOnEachServer(m, loadOnServer)
{
// Traverse the loadOnServer and
// print each loads
for (let i = 0; i < m; i++) {
document.write(i + 1, " st Server -> ", loadOnServer[i], ".<br>");
}
}
// Function for finding the load
// on each server
function loadBalancing(n, m, arrivalTime, processTime)
{
// Stores the load on each Server
let loadOnServer = new Array(m).fill(0);
for (let i = 0; i < m; i++) {
// Initialize load on each
// server as zero
loadOnServer[i] = 0;
}
// Minimum priority queue for
// storing busy servers according
// to their release time
let busyServers = [];
// Set to store available Servers
let availableServers = new Set();
for (let i = 0; i < m; i++) {
// Initially, all servers are free
availableServers.add(i);
}
let temp_arr = Array.from(availableServers).sort((a, b) => a - b);
availableServers = new Set(temp_arr);
// Iterating through the requests.
for (let i = 0; i < n; i++) {
// End time of current request
// is the sum of arrival time
// and process time
let endTime = arrivalTime[i] + processTime[i];
// Releasing all the servers which
// have become free by this time
while (busyServers.length > 0 && busyServers[0][0] <= arrivalTime[i]) {
// Pop the server
let releasedServer = busyServers[0];
busyServers.shift();
// Insert available server
availableServers.add(releasedServer[1]);
temp_arr = Array.from(availableServers).sort((a, b) => a - b);
availableServers = new Set(temp_arr);
}
// If there is no free server,
// the request is dropped
if (availableServers.length > 0) {
continue;
}
let demandedServer = i % m;
let availableServers_temp = Array.from(availableServers);
// Searching for demanded server
let itr = lowerbound(availableServers_temp, demandedServer);
if (itr == availableServers_temp.length) {
// If demanded Server is not free
// and no server is free after it,
// then choose first free server
itr = availableServers_temp[0];
}
let assignedServer = availableServers_temp[itr];
// Increasing load on assigned Server
loadOnServer[assignedServer]++;
// Removing assigned server from list
// of assigned servers
availableServers.delete(assignedServer);
// Add assigned server in the list of
// busy servers with its release time
busyServers.push([endTime,assignedServer]);
busyServers.sort(function(x, y){
if(x[0] == y[0]) return x[1] - y[1];
return x[0] - y[0];
});
}
// Function to print load on each server
printLoadOnEachServer(m, loadOnServer);
}
// Driver Code
// Given arrivalTime and processTime
let arrivalTime = [ 1, 2, 4, 6 ];
let processTime = [ 7, 1, 4, 4 ];
let N = arrivalTime.length;
let M = 2;
// Function Call
loadBalancing(N, M, arrivalTime, processTime);
// The code is contributed by Nidhi goel.
</script>
Python3
# Python code for the above approach
import heapq
def print_load_on_each_server(m, load_on_server):
"""
Function to print load on each server
"""
for i in range(m):
print(f"{i+1}st Server -> {load_on_server[i]}.")
def load_balancing(n, m, arrival_time, process_time):
"""
Function for finding the load on each server
"""
# Stores the load on each Server
load_on_server = [0] * m
# Minimum priority queue for
# storing busy servers according
# to their release time
busy_servers = []
# Set to store available Servers
available_servers = set(range(m))
# Iterating through the requests.
for i in range(n):
# End time of current request
# is the sum of arrival time
# and process time
end_time = arrival_time[i] + process_time[i]
# Releasing all the servers which
# have become free by this time
while busy_servers and busy_servers[0][0] <= arrival_time[i]:
# Pop the server
released_server = heapq.heappop(busy_servers)
# Insert available server
available_servers.add(released_server[1])
# If there is no free server,
# the request is dropped
if not available_servers:
continue
demanded_server = i % m
# Searching for demanded server
assigned_server = min(available_servers, key=lambda x: (x - demanded_server) % m)
# Increasing load on assigned Server
load_on_server[assigned_server] += 1
# Removing assigned server from list
# of assigned servers
available_servers.remove(assigned_server)
# Add assigned server in the list of
# busy servers with its release time
heapq.heappush(busy_servers, (end_time, assigned_server))
# Function to print load on
print_load_on_each_server(m, load_on_server)
if __name__ == "__main__":
# Given arrivalTime and processTime
arrival_time = [1, 2, 4, 6]
process_time = [7, 1, 4, 4]
n = len(arrival_time)
m = 2
# Function Call
load_balancing(n, m, arrival_time, process_time)
# This code is contributed by sdeadityasharma
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
// C# Program for the above approach
// Implementing sort in 2d list.
class GFG : IComparer<List<int>>
{
public int Compare(List<int> x, List<int> y)
{
if(x[0] == y[0])
{
return x[1].CompareTo(y[1]);
}
// CompareTo() method
return x[0].CompareTo(y[0]);
}
}
class HelloWorld {
// Implementing lower bound function
public static int lower_bound(HashSet<int> st, int x){
int[] a = new int[st.Count];
int i = 0;
foreach(var val in st){
a[i] = val;
i = i + 1;
}
int l = 0;
int h = a.Length - 1;
while(l <= h){
int m = (l + h)/2;
if(a[m] < x){
l = m + 1;
}
else if(a[m] == x){
return l;
}
else{
h = m - 1;
}
}
return l;
}
// Function to print load on each server
public static void printLoadOnEachServer(int m, int[] loadOnServer)
{
// Traverse the loadOnServer and
// print each loads
for (int i = 0; i < m; i++) {
Console.Write(i+1);
Console.Write(" st Server -> ");
Console.WriteLine(loadOnServer[i] != i ? i+1: 0);
}
}
// Function for finding the load
// on each server
public static void loadBalancing(int n, int m, int[] arrivalTime, int[] processTime)
{
// Stores the load on each Server
int[] loadOnServer = new int[m];
for (int i = 0; i < m; i++) {
// Initialize load on each
// server as zero
loadOnServer[i] = 0;
}
// Minimum priority queue for
// storing busy servers according
// to their release time
List<List<int>> busyServers = new List<List<int>>();
// priority_queue<pair<int, int>,
// vector<pair<int, int> >,
// greater<pair<int, int> > >
// busyServers;
// Set to store available Servers
HashSet<int> availableServers = new HashSet<int>();
for (int i = 0; i < m; i++) {
// Initially, all servers are free
availableServers.Add(i);
}
// Iterating through the requests.
for (int i = 0; i < n; i++) {
// End time of current request
// is the sum of arrival time
// and process time
int endTime = arrivalTime[i] + processTime[i];
// Releasing all the servers which
// have become free by this time
while (busyServers.Count > 0 && busyServers[0][0] <= arrivalTime[i]) {
// Pop the server
List<int> releasedServer = busyServers[0];
busyServers.RemoveAt(0);
// Insert available server
availableServers.Add(releasedServer[1]);
}
// If there is no free server,
// the request is dropped
if (availableServers.Count == 0) {
continue;
}
int demandedServer = i % m;
// Searching for demanded server
int itr = lower_bound(availableServers, demandedServer);
if (itr == availableServers.Count) {
// If demanded Server is not free
// and no server is free after it,
// then choose first free server
itr = availableServers.Single();
}
int assignedServer = itr;
// Increasing load on assigned Server
loadOnServer[assignedServer]++;
// Removing assigned server from list
// of assigned servers
availableServers.Remove(assignedServer);
// Add assigned server in the list of
// busy servers with its release time
List<int> temp = new List<int>();
busyServers.Add(temp);
busyServers[busyServers.Count - 1].Add(endTime);
busyServers[busyServers.Count - 1].Add(assignedServer);
GFG gg = new GFG();
busyServers.Sort(gg);
}
// Function to print load on each server
printLoadOnEachServer(m, loadOnServer);
}
static void Main() {
// Given arrivalTime and processTime
int[] arrivalTime = { 1, 2, 4, 6 };
int[] processTime = { 7, 1, 4, 4 };
int N = arrivalTime.Length;
int M = 2;
// Function Call
loadBalancing(N, M, arrivalTime, processTime);
}
}
// The code is contributed by Arushi Jindal.
Output: 1st Server -> 1.
2st Server -> 2.
Time Complexity: O(N*log M)
Auxiliary Space: O(M)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem