Implement a random fixed-size load balancer
Problem: Implement a Random Fixed-Size Load Balancer
Design and implement a minimal in-memory load balancer. This load balancer must route each incoming request to one backend node, chosen uniformly at random from its active set.
Requirements
- The load balancer will maintain a collection of backend nodes, identified by unique strings or IDs.
- The total number of nodes is strictly bounded by a fixed maximum capacity,
K. - The following operations must be supported:
addNode(nodeId):- Adds a node if it is not already present in the load balancer.
- If the load balancer has reached its maximum capacity
K, the behavior must be explicitly defined and documented (e.g., reject the addition, throw an exception).
removeNode(nodeId):- Removes the specified node if it is currently present.
- If the node is not found, the operation should perform no action (no-op).
route()(orgetNodeForRequest()):- Returns a node chosen uniformly at random from the set of currently active nodes.
- If there are no active nodes, the behavior must be explicitly defined and documented (e.g., return null, throw an exception).
Constraints and Edge Cases
- Handling attempts to add a node that is already present.
- Gracefully managing requests to remove a node that does not exist.
- Defining behavior when
route()is called with no active nodes. - Ensuring the
route()operation is highly efficient, ideally achieving O(1) time complexity.
Deliverable
Provide a clear description of the chosen data structures and an implementation of the API. The implementation can be language-agnostic (pseudocode) or follow a Java-style syntax.
AI interview prep
Core dilemma
Achieving O(1) average time complexity for all three core operations (`addNode`, `removeNode`, `route`) while maintaining uniform random selection and respecting a fixed capacity `K`.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a random fixed-size load balancer
Problem: Implement a Random Fixed-Size Load Balancer
Design and implement a minimal in-memory load balancer. This load balancer must route each incoming request to one backend node, chosen uniformly at random from its active set.
Requirements
- The load balancer will maintain a collection of backend nodes, identified by unique strings or IDs.
- The total number of nodes is strictly bounded by a fixed maximum capacity,
K. - The following operations must be supported:
addNode(nodeId):- Adds a node if it is not already present in the load balancer.
- If the load balancer has reached its maximum capacity
K, the behavior must be explicitly defined and documented (e.g., reject the addition, throw an exception).
removeNode(nodeId):- Removes the specified node if it is currently present.
- If the node is not found, the operation should perform no action (no-op).
route()(orgetNodeForRequest()):- Returns a node chosen uniformly at random from the set of currently active nodes.
- If there are no active nodes, the behavior must be explicitly defined and documented (e.g., return null, throw an exception).
Constraints and Edge Cases
- Handling attempts to add a node that is already present.
- Gracefully managing requests to remove a node that does not exist.
- Defining behavior when
route()is called with no active nodes. - Ensuring the
route()operation is highly efficient, ideally achieving O(1) time complexity.
Deliverable
Provide a clear description of the chosen data structures and an implementation of the API. The implementation can be language-agnostic (pseudocode) or follow a Java-style syntax.