00:00

Problem Statement

Input/Output Examples

Visual Explanation

Virtual nodes are what make the split fair: with a single point per server, the arcs between points can differ hugely in size.

Approaches

Both approaches give the same owner for every key, checked on 300 random sequences of node changes. The first stores the ring in a TreeMap; the second in sorted arrays.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Both rings answer a lookup with a binary search over p points. The TreeMap updates in place when a server joins or leaves; the arrays are rebuilt, which is cheap because it happens rarely.

Interview Tips and Recommended Approach

  • Start with the problem: going from n to n + 1 servers with modulo hashing moves about n / (n + 1) of the keys.
  • Explain virtual nodes; they even out the load and spread a failed server's keys over all the others.
  • Use a well-spread hash, such as MD5 or MurmurHash. String.hashCode() clusters similar names.
  • Name real uses: distributed caches, sharded databases such as Cassandra and DynamoDB, and load balancers.

Recommended Approach: TreeMap Ring

Why: the TreeMap gives the clockwise lookup in one call and lets servers join or leave without rebuilding the whole ring.