Session Manager Load Balancing Implementation
Palantir interview questions · Medium · Coding round · reported 1× · 6 tests
Implement a SessionManager that distributes sessions across three servers:
s1, s2, and s3.
Interface
class SessionManager {
start_session(sessionId: string): void;
get_allocation(): Record<string, Set<string>>;
}
Rules
start_session(sessionId)assigns the session to the server currently holding the fewest sessions.- If multiple servers are tied for the fewest sessions, break ties by server name order:
s1, thens2, thens3. - If the same
sessionIdis submitted again, silently ignore it. get_allocation()returns the current mapping from server ID to the set of assigned session IDs.- The allocation must always stay balanced, so the difference between the most-loaded and least-loaded server is never more than
1.
Run your code with Ctrl+Enter to see results.