Load Balancer
Difficulty: MEDIUMCategory: DSAFrequency: 1Asked On 2025-06-02
Problem Statement
Every request has a weight. Find the machine currently holding the smallest weight. If there are ties, choose based on alphabetical order.
Example:
There is a class loadBalancer. Suppose we have servers a, b, and c. Implement a route method such that:
•lb.route(weight = 2) returns a
•lb.route(weight = 1) returns b
•lb.route(weight = 1) returns c
•lb.route(weight = 1) returns b
The last return of b is because both b and c have a request with weight 1 at that time, and we choose the server with the smallest alphabetical order.
Followup:
Each request now also has a processing time that you need to take in account. You need to subtract processed request’s weight after processing the load on a server. Example: route(2, 0.1) -> "a", route(1, 0.3) -> "b", sleep(0.1), route(2, 0.1) -> "a"