Code RoomPN-counter CRDT replica merge
MediumPrep Room Coding #261

PN-counter CRDT replica merge

CodingDistributed systemsMid–Senior~25 min

A PN-counter is a CRDT supporting increments and decrements that converges across replicas without coordination. Each replica keeps two grow-only maps per node: P (total increments per node) and N (total decrements per node). To merge a set of replica states you take, for every node, the maximum P and maximum N seen across all replicas; the counter's value is sum(P) - sum(N). Each replica state is given as [P_dict, N_dict]. Given a list of replica states, return the converged integer value after merging them all.

Implement
pn_counter_value(replicas: list[list]) → int
Examples
in[[[{"a":5,"b":2},{"a":1}],[{"a":3,"b":4},{"a":1,"c":2}]]]out6
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 25 min
InputExpectedGot
[[[{"a":5,"b":2},{"a":1}],[{"a":3,"b":4},{"a":1,"c":2}]]]6not run yetsample