Consistent hash ring
Implement createRing(replicas), the consistent hashing used to spread keys over cache or database nodes. It returns an object with addNode(name), removeNode(name) and getNode(key). A global hash(text) returns a whole number from 0 to 2^32 - 1. addNode puts replicas points on the ring for the node, at hash(name + '#' + i) for i from 0 to replicas - 1; removeNode takes them off. getNode returns the node owning the first point whose hash is at least hash(key), wrapping to the lowest point when there is none, or null when the ring is empty. In the tests an op [at, method, ...args] calls that method at virtual time at (ms); the answer lists what each call returned. Only getNode returns something.
Start from
function createRing(replicas) {
// Return { addNode, removeNode, getNode } on a ring of hashes.
}Hints and solution
The app has this lab in JavaScript, TypeScript and Python. This page shows the JavaScript code.
Solve it in the app: its tests run without a year.
The hints, the solution and what an interviewer looks for opens with a year in the app. What a year includes