Design a server load balancer that assigns each client to the least-loaded server
Company: Snowflake
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a server load balancer that spreads client connections across a fixed set of servers and keeps them balanced. The balancer manages `numServers` servers with IDs `0` to `numServers - 1`, and every server has the same maximum `capacity` (the most clients it can hold at once).
When a new client connects, assign it to the server with the least current load (fewest active clients). If several servers share that load, pick the one with the smallest ID. Clients can also disconnect, which frees one unit of capacity on their server.
Internally, use a priority queue (min-heap) to select the least-loaded server for each new connection.
Implement a class `ServerLoadBalancer(numServers, capacity)` with these methods:
- `addClient(clientId: int) -> int`: assign the client to the least-loaded server (smallest ID on ties) and return that server's ID. Return `-1` if every server is full or the client is already connected.
- `removeClient(clientId: int) -> bool`: remove the client from its server, freeing one unit of capacity. Return `True` if the client was found and removed, `False` otherwise.
- `getClientServer(clientId: int) -> int`: return the ID of the server the client is on, or `-1` if the client is not connected.
- `getServerLoad(serverId: int) -> int`: return the current number of clients on the server, or `-1` if `serverId` is not a valid ID.
Example 1, with 3 servers of capacity 2:
```text
ServerLoadBalancer(3, 2)
addClient(10) -> 0 loads [1, 0, 0]
addClient(11) -> 1 loads [1, 1, 0]
addClient(12) -> 2 loads [1, 1, 1]
addClient(13) -> 0 loads [2, 1, 1] all tied at 1, smallest ID wins
addClient(11) -> -1 client 11 is already connected
getClientServer(13) -> 0
getServerLoad(0) -> 2
getServerLoad(3) -> -1 there is no server 3
removeClient(11) -> True loads [2, 0, 1]
getClientServer(11) -> -1
addClient(14) -> 1 loads [2, 1, 1] server 1 is now the least loaded
```
Example 2, with 2 servers of capacity 1:
```text
ServerLoadBalancer(2, 1)
addClient(1) -> 0 loads [1, 0]
addClient(2) -> 1 loads [1, 1]
addClient(3) -> -1 both servers are full
removeClient(1) -> True loads [0, 1]
removeClient(1) -> False client 1 is no longer connected
addClient(3) -> 0 loads [1, 1]
getClientServer(3) -> 0
```
```hint Updating a priority
Python's `heapq` cannot change the priority of an entry already inside the heap. Think about pushing a new entry when a server's load changes, and how you would later recognize the outdated one.
```
```hint When everything is full
All servers share one capacity. If the least-loaded server is full, what does that say about the rest, and do you need to touch the heap at all?
```
### Constraints & Assumptions
- `1 <= numServers <= 1000`
- `1 <= capacity <= 1000`
- Valid server IDs satisfy `0 <= serverId < numServers`; `getServerLoad` must return `-1` for any other value.
- `clientId` values are unique non-negative integers, one per client.
- At most `10^4` method calls in total.
### Clarifying Questions to Ask
- If `addClient` is called for a client that is already connected, should its current assignment stay exactly as it is?
- When a client disconnects and later reconnects, is it placed fresh by the current loads, or should it return to its old server?
- Is the set of servers fixed for the lifetime of the object, or can servers be added and removed?
- Will the balancer be called from more than one thread?
### What a Strong Answer Covers
- The state kept (per-server load, client-to-server map, heap) and the invariant that keeps the heap consistent with the true loads.
- How `addClient` finds the smallest `(load, id)` pair and handles full servers and repeated client IDs.
- How `removeClient` lowers a server's load without searching the heap, and how outdated heap entries are detected later.
- The time and space cost of each method, including how the heap grows as clients come and go.
- Correct return values for every edge case: invalid server ID, unknown client, removing the same client twice.
### Follow-up Questions
- The balancer runs for months with millions of connects and disconnects. How do you stop the heap from growing without bound?
- Servers now have different capacities. What changes in how you pick a server and in how you detect that no server has room?
- How would you add or retire a server at runtime, and what happens to the clients on a retired server?
- How would you make the class safe to call from many threads at once?
Overview: Implement a load balancer class that assigns each new client to the server with the fewest active clients, breaking ties by the smallest server ID, and supports disconnects and load lookups. It tests keeping a min-heap consistent with changing loads, capacity checks, and careful handling of edge cases.