KatInterview

Round-Robin Load Balancer with Capacity and Shutdown

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designsimulationhash-tableround-robin

Last updated

Tests stateful system simulation with round-robin target selection, connection lifecycle tracking, capacity enforcement, and target shutdown with eviction ordering.

You are building a load balancer that distributes client connections across multiple backend servers.

The system manages N backend servers, numbered from 0 to N - 1. Each server can handle at most max_capacity simultaneous connections.

Your task is to process a sequence of commands that modify the load balancer's state. All commands execute in order on a single thread.

Commands

connect

["connect", connection_id]

Assign a new connection to a backend server using round-robin selection, or return the existing assignment if this connection is already active.

Behavior:

  • If connection_id is already assigned to a server, return that server's index without changing any state
  • Otherwise, starting from the current round-robin position, search in circular order for the first server that:
    • Has not been shut down
    • Has fewer than max_capacity active connections
  • If such a server is found:
    • Assign the connection to that server
    • Move the round-robin pointer to the next position (wrapping around to 0 after N-1)
    • Return the server's index
  • If no server can accept the connection, return -1 without changing any state

disconnect

["disconnect", connection_id]

Remove an active connection.

Behavior:

  • If connection_id is currently assigned to a server:
    • Remove the connection
    • Free up one slot on that server
    • Return true
  • Otherwise, return false

The round-robin pointer is never affected by disconnect operations.

shutdown

["shutdown", server_index]

Take a server offline and evict all its active connections.

Behavior:

  • If the server is already shut down, return an empty list
  • Otherwise:
    • Mark the server as unavailable for future connections
    • Collect all connection IDs currently on that server
    • Remove all those connections from the system
    • Return the list of evicted connection IDs in the order they were originally connected to that server

Once a server is shut down, it remains unavailable for the rest of the simulation.

Output

Return a list containing one result per command:

  • connect returns an integer (server index or -1)
  • disconnect returns a boolean
  • shutdown returns a list of connection IDs

Constraints

  • 1 ≤ N ≤ 10,000
  • 0 ≤ max_capacity ≤ 1,000
  • 1 ≤ number of operations ≤ 50,000
  • Connection IDs are strings or integers, unique per logical connection
  • All operations are processed sequentially

Examples

Example 1

Input: process_load_balancer_commands(3, 2, [['connect', 'a'], ['connect', 'b'], ['connect', 'c'], ['connect', 'd'], ['connect', 'e'], ['disconnect', 'b'], ['connect', 'f'], ['shutdown', 0], ['connect', 'g']])

Output: [0, 1, 2, 0, 1, True, 2, ['a', 'd'], 1]

Hints

Hint 1

Use a hash map to track which server each connection is assigned to for O(1) duplicate detection

Hint 2

Maintain an insertion-ordered collection per server to support eviction in connection-creation order

Hint 3

Consider a segment tree or similar structure to efficiently find the next available server in round-robin order, avoiding O(N) scans when most servers are full or shut down