Your question is Thread-Safe In-Memory Load Balancer. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Implement an in-memory request load balancer for a Revolut service. It must route requests using either random selection or round-robin selection while safely handling concurrent routing and server membership updates.
Implement load_balancer(initial_servers, algorithm, operations, seed). The function must process operations through a thread-safe LoadBalancer implementation and return the server selected for each route operation.
initial_servers is a list of unique non-empty strings.algorithm is either "random" or "round_robin".operations is a list of dictionaries:
{"op": "route"} routes one request.{"op": "add", "server": server} adds a server if absent.{"op": "remove", "server": server} removes a server if present.{"op": "set_algorithm", "algorithm": algorithm} changes the routing algorithm.seed is an integer used to initialize the pseudo-random generator.None, for every route operation.All operations must be safe when invoked concurrently. A route must observe a consistent server collection and algorithm. Adding the same server twice and removing an unknown server are no-ops. Routing with no available servers returns None.
For round-robin routing, selections continue cyclically across requests. Removing a server must not skip or duplicate the remaining servers.
def load_balancer(initial_servers, algorithm, operations, seed):