← All Exercises

Own Keys on a Consistent Hash Ring

advancedjavascript~30min

Background

Consistent hashing limits key movement when nodes join or leave. Virtual nodes smooth the distribution; without them a single ring position can own most of the space.

Problem

Build a consistent hash ring with virtual nodes, assign each key to the first node at or after its hash, and report per-node key counts.

Input

JSON object: { nodes: string[], keys: string[], vnodes?: number } (default 3 virtual nodes)

Output

JSON object: { owners: [{ key, node }], load: [{ node, keys }] }

Sample tests

Sample 1

Input: {"nodes":["a"],"keys":["k1","k2","k3"]}
Expected: {"owners":[{"key":"k1","node":"a"},{"key":"k2","node":"a"},{"key":"k3","node":"a"}],"load":[{"node":"a","keys":3}]}

Your solution

Learning objective

Reason about key placement and rebalancing when ring membership changes.