Sticky load balancer for long-lived connections
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's HackerRank assessment, last asked Feb 2026. It tests simulation, load balancing and state machines.
Simulate a load balancer that places long-lived WebSocket connections on m backend servers. You process requests one by one and return a log of every successful placement as "connectionId serverIndex". The problem has five parts; each keeps every rule from the parts before it.
Placement rule. Pick the eligible server with the fewest active connections; break ties by the smaller index.
Part 1: connect
CONNECT connectionId places the connection and logs it.
Part 2: disconnect
DISCONNECT connectionId removes an active connection and frees its slot. It logs nothing. Ignore unknown ids.
Part 3: stickiness
Requests become CONNECT connectionId objectId. If any active connection has the same objectId, the new one must go to that server, even if it is not the least loaded. Once an object has no active connections, it is placed by load again.
Part 4: capacity
You get capacity[i] for each server. A full server is not eligible. A CONNECT with no eligible server, or whose sticky server is full, is rejected: no log line, no state change.
Part 5: shutdown
SHUTDOWN s evicts every connection on server s, then places them again one by one with the normal rules, in the order they arrived on s. Server s cannot receive any of them. Each successful placement is logged; a connection that cannot be placed is dropped. Afterwards s is available again, empty.
Interface
Unless you agree on a different interface with the interviewer, assume simulate(capacity: list[int], requests: list[str]) -> list[str] (in Parts 1 to 3, every capacity is unlimited).
Example
capacity = [2, 2, 1]
CONNECT c1 doc1 -> "c1 0"
CONNECT c2 doc2 -> "c2 1"
CONNECT c3 doc1 -> "c3 0" # sticky to server 0
CONNECT c4 doc3 -> "c4 2"
CONNECT c5 doc4 -> "c5 1" # server 0 is full
CONNECT c6 doc5 -> rejected # every server is full
DISCONNECT c2
SHUTDOWN 0 -> "c1 1" # c3 then sticks to server 1, which is full: dropped
log = ["c1 0", "c2 1", "c3 0", "c4 2", "c5 1", "c1 1"]
Constraints
-
1 <= m <= 10^5, up to2 * 10^5requests,0 <= capacity[i] <= 10^9. - Connection ids are unique among active connections.
Clarifying questions
- After a shutdown, does an evicted object stay sticky to its old server, or is stickiness rebuilt as connections are placed again?
- Is a rejected sticky connection retried anywhere else, or simply refused?
- Can a
SHUTDOWNname a server that is already empty? - What should happen to a
DISCONNECTfor a connection dropped during a shutdown?
What a strong answer covers
- A min-heap of
(load, index)with lazy deletion (or an ordered set) so each placement is O(log m), instead of scanning all servers. - Maps from connection to (server, object), from object to server, and a count per object to know when stickiness ends.
- Per-server arrival order that survives disconnects, so shutdown evicts in the right order.
- Checking the sticky server directly instead of searching the heap; rejecting with no partial state change.
- Tests built part by part, including ties, full servers and shutdown of a server that held a sticky object.
Follow-ups
- Servers can be added while the simulation runs.
- Shutdown drains gradually: at most
kconnections move per step. - Replace least-connections with weighted capacity or consistent hashing, and compare.
Where it's asked
- Stripe · HackerRank assessment · Last asked Feb 2026Leveled HackerRank task; test cases map to the parts in order
What it tests
simulation · load balancing · state machines
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
