Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Thread-Safe In-Memory Load Balancer

HardPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Formal Specification

  • 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.
  • Return a list containing one server name, or 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.

Constraints

  • 0 <= len(initial_servers) <= 10^4
  • 0 <= len(operations) <= 10^5
  • Initial server names are unique non-empty strings
  • Each operation contains a valid operation name and required fields
  • Expected individual operation cost is O(1), excluding list removal shifts and lock contention

Function Signature

def load_balancer(initial_servers, algorithm, operations, seed):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output