Stage 1 of 3: Keys on a ring
Background. A consistent-hash ring spreads keys over workers. Each worker sits at several points on a circle of positions, and a key goes to the worker whose point comes first at or after the key's own position. A worker that joins or leaves then changes the worker of only the keys next to its own points.
Implement HashRing in ring.py: HashRing(points_per_worker), add_worker(worker_id), remove_worker(worker_id) and worker_for(key).
Positions come from position(text) in ringhash.py: an integer from 0 to RING_SIZE - 1, where RING_SIZE is 2 ** 32.
add_worker(worker_id)putspoints_per_workerpoints on the ring for the worker: pointisits atposition(f"{worker_id}:{i}"), forifrom 0 topoints_per_worker - 1. It is never called for a worker already on the ring.remove_worker(worker_id)takes every point of the worker off the ring. For a worker not on the ring it does nothing and raises nothing.worker_for(key)returns the worker of the first point whose position is at leastposition(key). When no point is that high, it wraps around and returns the worker of the lowest point. On a ring with no points it returnsNone.- Two points can share a position. They are taken in order of worker id, plain
strorder ("c10"before"c9"): a key that reaches that position goes to the smaller id, and once that worker is removed, to the other.
Example
ring = HashRing(2)
ring.add_worker("oak")
ring.add_worker("elm")
ring.add_worker("ash")
ring.worker_for("order-100") # "ash"
ring.worker_for("order-112") # "oak"
ring.worker_for("order-104") # "elm": no point is that high, so the lowest point, elm:0
ring.remove_worker("ash")
ring.worker_for("order-100") # "oak": ash:0 is gone, and oak:0 comes next
The points and keys in ring order:
| On the ring | position() |
|---|---|
| point elm:0 | 98,977,715 |
| key order-100 | 760,767,782 |
| point ash:0 | 904,903,776 |
| key order-112 | 1,610,222,734 |
| point oak:0 | 2,073,473,868 |
| point ash:1 | 2,078,700,507 |
| point elm:1 | 2,154,310,927 |
| point oak:1 | 3,460,922,416 |
| key order-104 | 3,910,197,293 |
Notes
points_per_workeris 1 to 200. A worker id is 1 to 30 characters of ASCII letters, digits,-and_; a key is any string of 0 to 100 characters, the empty string included.- Several rings can exist at once, each with its own workers.
- On the starter, all 8 tests in
tests/test_part1.pyfail.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
ring.py - Read-only:
ringhash.pyand alltests/test_*.pyfiles - Worker ids are 1 to 30 characters of ASCII letters, digits,
-and_; a key is any string of 0 to 100 characters
What comes after stage 1
The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Weights and workers that go down. Some workers should carry more keys than others, and a worker can go down for a while without leaving the ring.
- Stage 3: Several workers per key. Each key needs a list of different workers, in a fixed order, even when some are down.
The files you get
- Yours to edit: ring.py.
- Read-only: ringhash.py, tests/__init__.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: no AI, an assistant that only answers
DoorDash runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.
Frequently asked, briefly answered
Is Consistent Hashing free?
It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.
Which company asks something like this?
DoorDash. It is modelled on DoorDash's coding round, which runs without AI. Based on candidates' reports of DoorDash interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.
What does the interviewer ask?
6 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.
More like it: every practice problem, DoorDash's interviews, the AI coding interview guide.