Code RoomPlace shard replicas
EasyPrep Room Coding #4814

Place shard replicas

CodingDatabases & SQLDistributed systemsEntry–Mid~16 min

A placement policy decides which machines hold the copies of one shard, and it spreads them across racks so that losing a rack of power cannot take the shard offline. node_names lists the machines in ring order and rack_names[i] names the rack that node_names[i] sits in. The policy starts at ring position start_index, then walks forward one position at a time, wrapping past the last position back to position 0, and it visits every machine exactly once. It takes a machine when no machine it has already taken sits in that rack, and skips the machine otherwise. It stops the moment it holds replica_count machines. If the walk finishes with fewer than that, the shard cannot be placed safely, so the policy gives up and returns an empty list. Return the chosen machine names in the order chosen. Return an empty list when replica_count is 0 or less. start_index is a valid position whenever at least one machine is given.

Implement
rack_aware_replicas(node_names: list[str], rack_names: list[str], start_index: int, replica_count: int) → list[str]
Examples
in[["n1","n2","n3","n4","n5","n6"],["r1","r1","r2","r2","r3","r3"],0,3]out["n1","n3","n5"]
in[["n1","n2","n3","n4","n5","n6"],["r1","r1","r2","r2","r3","r3"],1,3]out["n2","n3","n5"]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 16 min
InputExpectedGot
[["n1","n2","n3","n4","n5","n6"],["r1","r1","r2","r2","r3","r3"],0,3]["n1","n3","n5"]not run yetsample
[["n1","n2","n3","n4","n5","n6"],["r1","r1","r2","r2","r3","r3"],1,3]["n2","n3","n5"]not run yetsample