Drill

ProblemsC# › network

Pick the next server in round-robin

easynetworkMathC#

A load balancer cycles through servers by index. Given the current slot and the total count, return the next one.

RoundRobinNext(current: int, servers: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int RoundRobinNext(int current, int servers) {
    
}

Worked examples

CallResult
RoundRobinNext(0, 5)1
RoundRobinNext(4, 5)0
RoundRobinNext(0, 1)0
RoundRobinNext(3, 0)0

Hint

Modulo wraps the index back to zero.

Reference solution in C#
public int RoundRobinNext(int current, int servers) {
    if (servers <= 0) return 0;
    return (((current + 1) % servers) + servers) % servers;
}

The same problem in another language

More network problems in C#