Worker heartbeat eviction

Write codeEasy18 min

Your task

A cluster coordinator drops workers that stop sending heartbeats. Every id in worker_ids is distinct, joins at second 0 and counts as last seen at second 0. beats holds heartbeats written as "second|worker" in non decreasing second order, and every heartbeat names a worker on the roster. The coordinator sweeps the roster at the second of each heartbeat, before recording that heartbeat, and once more at final_second, which is never earlier than the last heartbeat. A sweep at second t evicts every live worker whose last seen second is more than timeout seconds before t, so t minus last seen must be greater than timeout and an exact match survives. Within one sweep the coordinator evicts the longest silent worker first, breaking ties by the order the ids appear in worker_ids. Eviction is final, so a heartbeat from an evicted worker is ignored and a heartbeat arriving after its own worker has timed out does not save it. Return the evicted ids in eviction order.

Examples

evicted_worker_order(worker_ids: list[str], beats: list[str], timeout: int, final_second: int) -> list[str]
Example 1
Inputevicted_worker_order(["w1", "w2", "w3"], ["5|w1", "5|w2", "12|w1", "12|w2"], 10, 20)
Output["w3"]
Example 2
Inputevicted_worker_order(["alpha", "beta", "gamma"], ["3|beta", "5|alpha"], 8, 20)
Output["gamma", "beta", "alpha"]
Example 3
Inputevicted_worker_order(["w1", "w2"], ["4|w1", "4|w2"], 3, 4)
Output["w1", "w2"]