AlgoMaster Logo
AlgoMasterPlace Rack-Aware Replicasmedium

Place Rack-Aware Replicas

medium

A distributed file system stores multiple replicas of each block so data remains available when hardware fails. Replicas on the same rack share power and networking, so placing all copies together leaves them vulnerable to one rack-level failure.

Design a RackAwareReplicaPlacer class:

  • RackAwareReplicaPlacer() creates a stateless placement utility.
  • int[] place(int[] rackCapacities, int numReplicas) returns how many replicas are placed on each rack.

rackCapacities[i] is the number of free replica slots on rack i. Place replicas in round-robin order by rack index:

  • Give at most one replica to each eligible rack per rotation.
  • Skip racks that have reached capacity.
  • Continue cycling until numReplicas replicas have been placed or every rack is full.

Return an array aligned with rackCapacities. Do not modify the input array, and treat each call independently.

Example 1:

Input:

Output:

Explanation: The first rotation places one replica on each rack. All three copies occupy different rack failure domains.

Example 2:

Input:

Output:

Explanation: The first three replicas use racks 0, 1, and 2. Racks 0 and 1 are then full, so rack 2 receives the fourth replica.

Constraints

  • 1 <= rackCapacities.length <= 10^5
  • 0 <= rackCapacities[i] <= 10^5
  • 0 <= numReplicas <= 10^5
  • At most 100 calls are made to place.
Hints

Loading...
CallReturns
new RackAwareReplicaPlacer()null
place([2,2,2], 3)[1,1,1]

The first round places one replica on each rack, so all three copies occupy different failure domains.

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