Drill

ProblemsPython › network

Pick the next server in round-robin

easynetworkPython

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

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

Solve it in the editor →

Where you start

def round_robin_next(current: int, servers: int) -> int:
    

Worked examples

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

Hint

Modulo wraps the index back to zero.

Reference solution in Python
def round_robin_next(current: int, servers: int) -> int:
    if servers <= 0:
        return 0
    return ((current + 1) % servers + servers) % servers

The same problem in another language

More network problems in Python