AlgoMaster Logo
AlgoMasterResolve a Quorum Readmedium

Resolve a Quorum Read

medium

A quorum read contacts a subset of replicas and resolves their responses to one value. In this model, a larger version is fresher. A fixed tie-break is required when two contacted replicas report the same version.

Design a QuorumReadResolver class:

  • QuorumReadResolver() creates a stateless resolver.
  • int resolve(int[] versions, int[] values, int[] readSet) returns the value selected from the contacted replicas.

versions[i] and values[i] describe replica i. Consider only indices in readSet. Choose the contacted replica with the greatest version. If several contacted replicas have that version, choose the smallest replica index. Return the chosen replica's value.

Example 1:

Input:

Output:

Explanation: The read contacts replicas 0, 2, and 4 with versions 3, 2, and 1. Replica 0 has the freshest contacted version.

Example 2:

Input:

Output:

Explanation: Replicas 2 and 1 tie on version 5. The smaller replica index is 1, so the result is values[1].

Constraints

  • 1 <= versions.length == values.length <= 10^5
  • 1 <= readSet.length <= versions.length
  • 0 <= versions[i] <= 10^9
  • -10^9 <= values[i] <= 10^9
  • Every readSet[i] is a valid replica index.
  • readSet contains no duplicate indices.
  • At most 100 calls are made to resolve.
Hints

Loading...
CallReturns
new QuorumReadResolver()null
resolve([3,1,2,3,1], [30,10,20,31,11], [0,2,4])30

The contacted versions are 3, 2, and 1. Replica 0 is freshest, so its value 30 is returned.

Run checks these cases. Submit also runs a larger hidden set.