Keep two sorted lists: waiting and shown
The candidate who reported the movie playlist rotated through the titles with two sorted lists, and another candidate's advice was two sorted maps (2 reports). Here's the idea. Split the keys by whether their title has shown in this loop:
waiting: keys of titles not yet shown, sorted, sowaiting[0]is the next title.shown: keys of titles already shown in this loop, also sorted.
get takes waiting[0], files it into shown, and returns its title. When waiting runs out, the loop is over: every key is in shown, already in order, so it becomes the new waiting, and shown starts empty.
| After call | waiting | shown |
|---|---|---|
| 0 | Comet, Harbor, Maple, Orbit, Tundra | |
| 2 | Maple, Orbit, Tundra | Comet, Harbor |
| 5 | Comet, Harbor, Maple, Orbit, Tundra | |
| 6 | Harbor, Maple, Orbit, Tundra | Comet |
The two lists answer the question the cursor couldn't. A title is shown if its key is in shown, whatever its score does next.
Inside a class, the refill is a swap: self.waiting, self.shown = self.shown, [], which is O(1). In a plain function that gets the two lists as arguments, you can't rebind the caller's names, so refill in place with waiting.extend(shown) and shown.clear().
waiting.pop(0) shifts every key left, so it costs O(n). That's fine at catalog sizes, and the next step measures it. A sorted container from a library, like Java's TreeMap, does it in O(log n).
The editor holds get(waiting, shown) as a plain function over the two lists. Write it: show the best waiting key, file it into shown, and refill waiting when it runs out.