Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Thread-Safe In-Memory Load Balancer
00:00
5 left

Thread-Safe In-Memory Load Balancer

HardPython

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):
Interviewer

Your question is Thread-Safe In-Memory Load Balancer. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.