Implement a random fixed-size load balancer
RevolutSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of randomized selection, efficient in-memory data structures, and API/state management for a fixed-capacity load balancer that must support addNode, removeNode, and uniform random routing while handling duplicates, removals, and empty-state behavior.
Problem: Build a simple load balancer
Implement a minimal in-memory load balancer that routes each incoming request to one backend node chosen uniformly at random.
Requirements
- The load balancer maintains a set of backend nodes (e.g., strings or IDs).
- The number of nodes is bounded by a fixed maximum capacity
K. - Support these operations:
addNode(nodeId)
- Adds a node if it is not already present.
- If the load balancer is at capacity
K, define and document what happens (e.g., reject the add / throw).
removeNode(nodeId)
- Removes the node if present; otherwise no-op.
route()(orgetNodeForRequest())
- Returns a node chosen uniformly at random from the currently active nodes.
- If there are no nodes, define and document what happens (e.g., return null / throw).
Constraints / Edge cases to handle
- Adding duplicates.
- Removing a non-existent node.
- Routing when empty.
- Keeping
route()efficient (ideally O(1)).
Deliverable
Describe the data structures and implement the API (language-agnostic or Java-style).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.