Code RoomConsistent hash rebalance
MediumPrep Room Coding #266

Consistent hash rebalance

CodingDatabases & SQLAlgorithms & data structuresMid–Senior~25 min

A cluster shards keys with a consistent-hashing ring (one ring point per node, no virtual nodes for this problem). A node id maps to ring position `hash31(node) % ring_size` and a key maps to `hash31(key) % ring_size`, using the FNV-1a hash in the reference; ownership is the first node clockwise (smallest position >= key position, wrapping). Given the current `nodes`, a `new_node` that joins, the `ring_size`, and the list of `keys`, return the number of keys whose owning node CHANGES as a result of the join (i.e., keys that must be moved). Assume new_node is not already present.

Implement
keys_moved_on_join(nodes: list[str], new_node: str, ring_size: int, keys: list[str]) → int
Examples
in[["A","B"],"C",1024,["k1","k2","k3","k4","k5"]]out4
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","B"],"C",1024,["k1","k2","k3","k4","k5"]]4not run yetsample