Easy · System Design
Load balancer
Given an initial list of unique server names and a sequence of next/add/remove calls, implement a round-robin load balancer that walks the list with a cursor: nextServer() returns the server at the cursor and advances the cursor cyclically (nothing for an empty pool), addServer(name) appends a name that is not already in the list, and removeServer(name) deletes it without making any other server skip a turn. Return the servers handed out by the nextServer calls.
Examples
Example 1
{
"servers": ["A", "B"],
"ops": [
{"kind": "next"},
{"kind": "next"},
{"kind": "next"},
{"kind": "add", "server": "C"},
{"kind": "next"}
]
}Output: A, B, A, B
Example 2
{
"servers": ["A", "B", "C"],
"ops": [
{"kind": "next"},
{"kind": "next"},
{"kind": "remove", "server": "A"},
{"kind": "next"},
{"kind": "remove", "server": "C"},
{"kind": "next"},
{"kind": "next"}
]
}Output: A, B, C, B, B
Rebuild it in the studio
Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.