AlgoMaster Logo
AlgoMasterSimulate Partition Quorumsmedium

Simulate Partition Quorums

medium

During a network partition, a replicated system may reach only some of its replicas. Quorum sizes determine which operations that partition can continue serving and whether independently formed quorums are guaranteed to overlap.

Design a PartitionQuorumSimulator class:

  • PartitionQuorumSimulator(int replicaCount, int readQuorum, int writeQuorum) stores N, R, and W.
  • String partitionStatus(int reachableReplicas) reports which operations one partition can serve.
  • boolean hasSafeQuorums() reports whether the configured quorums satisfy both overlap rules below.

A partition can serve a read when reachableReplicas >= R and a write when reachableReplicas >= W. Return exactly one of:

  • "READ_WRITE" when it can serve both.
  • "READ_ONLY" when it can serve only reads.
  • "WRITE_ONLY" when it can serve only writes.
  • "UNAVAILABLE" when it can serve neither.

For this exercise, the quorum configuration is safe only when:

The first rule guarantees that every read quorum intersects every completed write quorum. The second guarantees that two write quorums cannot be disjoint. Assume a standard quorum protocol reads the newest version from the replicas it contacts.

Example 1:

Input:

Output:

Explanation: Three replicas meet both quorum sizes. Two meet neither. Both strict overlap inequalities hold for N = 5, R = 3, and W = 3.

Example 2:

Input:

Output:

Explanation: Either half of a 2-2 partition can satisfy both quorum sizes. Because the halves can be disjoint, availability does not imply safe consistency: R + W and 2W equal N instead of exceeding it.

Constraints

  • 1 <= replicaCount <= 10^9
  • 1 <= readQuorum, writeQuorum <= replicaCount
  • 0 <= reachableReplicas <= replicaCount
  • Return the status strings exactly as written.
  • Constructor values do not change after creation.
  • At most 100 method calls are made per object.
Hints

Loading...
CallReturns
new PartitionQuorumSimulator(5, 3, 3)null
partitionStatus(3)"READ_WRITE"
partitionStatus(2)"UNAVAILABLE"
hasSafeQuorums()true

Three reachable replicas satisfy both quorum sizes. Two satisfy neither. The configuration is safe because 3 + 3 > 5 and 2 × 3 > 5.

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