Min heap was the right call here, store tuples of (session count, server id) and always assign the next session to the least loaded server.
Clarify requirements first, then design a session manager that maintains a count of active sessions per server and uses a greedy algorithm to assign each new session to the least-loaded server, ensuring the difference between any two servers' session counts never exceeds one. Implement start_session to create a session and assign it, and get_allocation to return the current distribution, possibly with a method to end sessions for completeness.
Pro tip: Mention that the greedy approach works because the load difference is bounded by one, and discuss how to handle server failures or dynamic scaling, showing awareness of real-world distributed systems.
Ask about the expected number of servers, whether servers can be added or removed dynamically, and if sessions can end. Confirm that the goal is to keep the difference in session counts between any two servers at most one.
Choose a data structure to track sessions per server, such as a map from server ID to session count, and a min-heap or sorted list to efficiently find the least-loaded server. Also maintain a mapping from session ID to server ID for session termination.
When a new session starts, assign it to the server with the fewest active sessions. If multiple servers have the same minimum, pick any (e.g., the first). Update the session count and the session-to-server mapping.
Return the current distribution of sessions across servers, either as a list of counts or a map. Ensure the invariant holds: max count - min count <= 1.
If sessions can end, implement an end_session method that decrements the server's session count and removes the session mapping. This maintains the balance for future allocations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.