Palantir logoPalantir
Coding·60 minFree preview

Session Manager

Implement a session manager that distributes unique session IDs across a fixed set of servers so that loads differ by at most one, with a start operation, an allocation snapshot, and your own tests. A practice extension adds end_session with rebalancing by migrating existing sessions and requires O(log S) updates.

SWE
scheduling
data-structure
testing
Frequency
Low
Last asked
2026-10-28
Stage
phone-screen

Problem

Implement a session manager that assigns sessions to a fixed set of servers. Each active session contributes a load of 1, and a server's load is the number of sessions assigned to it.

class SessionManager:
    def __init__(self, servers: list[str]): ...
    def start_session(self, session_id: str) -> None: ...
    def get_allocation(self) -> dict[str, set[str]]: ...
  • start_session(session_id) assigns a new session to a server. If the session is already active, do nothing — a duplicate must not change any count.
  • get_allocation() returns a mapping from every server ID (including empty servers) to its set of session IDs; every active session appears exactly once.
  • After every operation, the difference between the highest and lowest server loads must be at most 1. When several allocations are valid, any of them is acceptable.
  • Write your own tests covering ordinary allocation and edge cases, including duplicate session IDs.

Examples

  • For three servers [s1, s2, s3], balanced distributions include [8, 8, 8] and [8, 8, 7].
  • Loads [3, 3, 2], [2, 2, 2], and [1, 0, 0] are valid; [3, 3, 1] is invalid.
  • With servers ["s1", "s2", "s3"], starting a, b, c, d, e can produce {"s1": {"a", "d"}, "s2": {"b", "e"}, "s3": {"c"}} (loads [2, 2, 1]). Calling start_session("a") again leaves the allocation unchanged.

Variants

Extended practice version: ending sessions with rebalancing

A practice extension of the base problem (not part of the reported phone-screen prompt) adds end_session(session_id) -> None plus performance requirements:

  • Ending a nonexistent session is a no-op; an ended ID may be started again.
  • If removal breaks balance, move existing sessions between servers to restore it.
  • get_allocation() must return an independent snapshot.
  • Updates must not scan all servers or sessions; target O(log S) or better. Init and get_allocation() are exempt.
  • State worst-case vs average vs amortized bounds, the hash-table assumption, and whether memory grows with history.

Example: from {"s1": {"a", "d"}, "s2": {"b", "e"}, "s3": {"c"}}, end_session("c") alone gives [2, 2, 0] (invalid); one valid result is {"s1": {"a"}, "s2": {"b", "e"}, "s3": {"d"}}. With one server, end_session("x") on an empty manager is a no-op and x can be started, ended, and started again.

Pitfalls

  • Server initialization, allocation tie-breaking, and behavior with no servers are unspecified in the base prompt; clarify these before coding.
  • Valid allocations are not unique, so tests must not assert one exact mapping. Check instead that max − min load ≤ 1, the set of active IDs is correct, each active session appears exactly once, and every configured server is present.
  • In the extended version, a session that was moved during rebalancing must still be removed from its current server when it ends — keep a session → server index up to date on every move.
  • Returning internal sets directly lets a caller corrupt state; test that mutating a returned snapshot leaves the manager unchanged.

Preparation

  • In 20 minutes, implement start_session and get_allocation for a fixed nonempty server list and verify that every distinct session appears on exactly one server.
  • In 10 minutes, add tests for duplicate IDs, uneven totals, an empty allocation, and the agreed no-server behavior; assert the count difference after every insertion.
  • Then add end_session with rebalancing and run the extended test checklist: ending a nonexistent ID, ending without migration, ending with migration, ending a previously moved session, ending everything, and mutating a returned snapshot.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...