56A movie billboard that rotates titles by score

mediumpython
45:00

56 A movie billboard that rotates titles by score

Netflix software engineer candidates report a coding round on a movie billboard, also called the movie playlist: in an onsite round (1 report) and in a data platform loop, where the candidate called it a known question from other reports (1 report). The shape they describe: each call returns a movie, no movie comes back twice until every movie has had its turn, then the next loop starts, and scores can be updated along the way (1 report). A prep site's version of it, for ML engineers, is about relevance scores that change and avoiding immediate repeats (1 source). Both candidates steered away from a heap: one tried it first and got stuck for a long time, then used two sorted lists, and the other's advice was two sorted maps instead of a heap (2 reports). The follow-ups they report are multithreading (where the locks go) and running it in production: how you'd store and read the data (2 reports). The tie-break, the update rules and the cooldown below are our choices, made so every answer is exact. This is practice in that shape, not a leaked question.

Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/56_billboard_rotation -k level1. Earlier levels must keep passing.

Build Billboard in solution.py. Titles are strings and scores are ints. Order titles by score, highest first, and break a tie by title in alphabetical order (plain string order, so "Harbor" comes before "Maple").

Level 1. Billboard(scores) takes a dict from title to score. get() returns the next title to show: the best title not yet shown in the current loop. Each title shows once per loop. The get() call that finds every title already shown starts a new loop, in which every title is waiting again. With no titles at all, get() returns None.

Level 2. update(title, score) sets a title's score, or adds the title if it's new. It counts at once. A title still waiting in this loop takes its place by its new score. A title already shown in this loop stays shown, and its new score counts from the next loop. A new title is waiting in the current loop, even when every other title has already been shown in it. Never change the dict that was passed to Billboard.

Level 3. No title twice in a row. The only place it can happen is where one loop meets the next: an update can make the title shown last the best one of the new loop. So get() skips a title that was shown on the call just before, and shows the best waiting title after it instead. The skipped title stays waiting and is picked by its score as usual, which here means the very next call. With only one title, it shows every time.

Level 4. A longer cooldown. Billboard(scores, cooldown=1): a title shown in one of the last cooldown calls to get() is cooling, and get() shows the best waiting title that isn't. Level 3 is cooldown=1, and cooldown=0 turns the rule off. If every title still waiting in this loop is cooling, show the one shown longest ago. A cooldown below 0 raises ValueError.

Things to get right: the tie-break, where a title lives after an update (waiting or shown), a title added after the last one shown, and a skip that only delays a title. Know what each call costs, and why a heap makes update hard. Follow-ups from the reports: where the locks go once several threads call get and update, and how you'd store and read the scores in production (2 reports).

solution.py
Run the tests to see which pass.