Katkat

Implement a random fixed-size load balancer

RevolutSoftware Engineer · Technical Screen

Last updated

This question evaluates understanding of randomized selection, efficient in-memory data structures, and API/state management for a fixed-capacity load balancer that must support addNode, removeNode, and uniform random routing while handling duplicates, removals, and empty-state behavior.

Problem: Build a simple load balancer

Implement a minimal in-memory load balancer that routes each incoming request to one backend node chosen uniformly at random.

Requirements

  • The load balancer maintains a set of backend nodes (e.g., strings or IDs).
  • The number of nodes is bounded by a fixed maximum capacity K.
  • Support these operations:
  1. addNode(nodeId)
  • Adds a node if it is not already present.
  • If the load balancer is at capacity K, define and document what happens (e.g., reject the add / throw).
  1. removeNode(nodeId)
  • Removes the node if present; otherwise no-op.
  1. route() (or getNodeForRequest())
  • Returns a node chosen uniformly at random from the currently active nodes.
  • If there are no nodes, define and document what happens (e.g., return null / throw).

Constraints / Edge cases to handle

  • Adding duplicates.
  • Removing a non-existent node.
  • Routing when empty.
  • Keeping route() efficient (ideally O(1)).

Deliverable

Describe the data structures and implement the API (language-agnostic or Java-style).