A heap of deadlines finds the next lease to end
Checking the leases on every call is only cheap if each check is cheap. Scanning every lease costs O(n) per call, and a queue with 100,000 tasks in flight answers thousands of calls a second.
A heap of deadlines fixes that. Push (deadline, lease number, task id) when a task is reserved. The lease that ends soonest is always heap[0], so a check looks at one entry, and pops only the leases that really ended.
There's a catch. A heap can't remove an entry from the middle. When w1 acks a before its deadline, a's entry stays in the heap. Later, a might even be reserved again, with a new entry. When the old entry reaches the top, it must not end the new lease.
The fix is a lease number. Every reservation gets a new number from a counter, and the queue remembers the live number for each reserved task. When an entry is popped, it counts only if its number is still the task's live number. Otherwise it's stale, and you drop it. This is the lazy delete from the heaps lesson.
heap entries Entry 1 is stale: a's live lease is 2, not 0, so it's dropped. Entry 2 ends b's lease now. Entry 3 is a's live lease and stays.
Use the number, not the worker's name. The same worker can lose a lease and reserve the same task again, and its name alone would make the old entry look live.
The function pop_ended in the editor gets the deadline heap and current, each reserved task's live lease number. Write it, so it pops every entry that is due and skips the stale ones.