Stockroom Quorum Read
A hardware shop keeps each item's stock count on 3 replicas. Every write carries a version number one higher than the last, and counts as done once 2 replicas store it (W = 2). A read asks all 3 and waits for the first 2 answers (R = 2). Since 2 + 2 > 3, those answers always include a replica holding the latest completed write. A slow or down replica never answers.
This is the quorum read of leaderless key-value stores (see Replication and Design a Key-Value Store). It only works if the read picks the newest answer, not the first.
Your task
Write quorumRead(replies, r). replies lists what each replica sent, in arrival order. Each entry is [version, value], or null for a replica that timed out.
The coordinator takes the first r entries that are not null and ignores everything after them, even a newer version, because it has already answered. Return [version, value] for the highest version among those r replies. If fewer than r replicas answered, the read fails: return an empty list.
Two replies with the same version always carry the same value.
quorumRead(replies: int[][], r: int) → int[]
Examples
replies = [[4, 12], null, [5, 9]] r = 2
[5, 9]
The first reply is version 4 (12 in stock). The second replica timed out, so the coordinator keeps waiting. The third reply is version 5 (9 in stock). That makes 2 replies, and version 5 is newer, so the answer is 9. Returning the first reply, 12, would show stock that was already sold.
replies = [[7, 30], [6, 31], [8, 29]] r = 2
[7, 30]
The first two replies, versions 7 and 6, already make a quorum of 2, so the coordinator answers with version 7. Version 8 arrives too late to be seen. That is allowed: R + W > N only promises that the read sees the latest write that had finished, and a write still on its way to its second replica has not finished.
replies = [null, [3, 5], null] r = 2
[]
Only one replica answered, and the read needs 2. Returning version 3 anyway would drop the overlap guarantee, so the read fails and the caller can retry.
Constraints
- 1 ≤ replies.length ≤ 7
- 1 ≤ r ≤ replies.length
- 1 ≤ version ≤ 1,000,000,000
- −1,000,000 ≤ value ≤ 1,000,000
Start with brute force
Returning the highest version in the whole list is wrong, not slow: the coordinator answers once it has r replies, so later ones never reach it (the second example).
Returning the first non-null reply is a read with R = 1, and with W = 2 on 3 replicas it can miss the latest write (the first example).
Hints
0 of 4 shown- Hint 1
Which replies does the coordinator look at? What happens to one that arrives after it has
ranswers? - Hint 2
Walk the replies in order, skip the
nulls, and stop once you have countedrreal replies. - Hint 3
While walking, remember the reply with the highest version so far. Do not sort: arrival order is part of the input.
- Hint 4
If the walk ends with fewer than
rreplies, return[]. Otherwise return the remembered[version, value].
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The solution simulates the coordinator. It goes through the replies in arrival order, skips timeouts, remembers the newest version it has seen, and answers as soon as it has counted r replies.
Trace the first example, r = 2:
[4, 12]: best is version 4, counted 1.null: skipped, still 1.[5, 9]: 5 > 4, so best is version 5. Counted 2 = r: return[5, 9].
The limits: R + W > N guarantees the latest completed write, if both quorums come from the same N replicas. It does not order concurrent writes, and a read overlapping an unfinished write may see it while a later read does not. Read repair narrows that gap: the coordinator sends the newest value back to stale replicas it heard from.
function quorumRead(replies, r) {
let best = null, counted = 0;
for (const reply of replies) {
if (reply === null) continue; // timed out: never answered
if (best === null || reply[0] > best[0]) best = reply;
counted++;
if (counted === r) return [best[0], best[1]]; // quorum reached: answer now
}
return []; // fewer than r replicas answered
}def quorum_read(replies, r):
best, counted = None, 0
for reply in replies:
if reply is None: # timed out: never answered
continue
if best is None or reply[0] > best[0]:
best = reply
counted += 1
if counted == r: # quorum reached: answer now
return [best[0], best[1]]
return [] # fewer than r replicas answeredRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.