57No repeated titles on the first screen
57 No repeated titles on the first screen
Netflix software engineer candidates report a question about deduplicating the homepage. One got it in a technical screen: the first n titles of each row are deduplicated across the whole page, and past that a row only has to avoid repeating itself. The follow-ups were a different n for each row, and special rows that don't need the global dedup. That candidate also had to write their own test cases after the code (1 report). Another got it in a phone screen that was more about talking it through than about code: on the first screen, no row and no column may hold a repeated show (1 report). A third report lists only its title, deduping titles in a per-shelf viewport (1 source). The tie-breaks, what a row does when it runs out, and the exact form of the column rule are this problem's choices, not the reports'. 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/57_viewport_dedup -k level1. Earlier levels must keep passing.
The page. rows is a list of rows, top to bottom. Each row is a list of title ids, best first. The first
screen shows the first width slots of every row, and the rest of a row shows only when the viewer scrolls that row
sideways. Return the new rows as new lists, and never change the lists you're given.
Level 1. Write dedup_homepage(rows, width). Go down the rows in order, and fill each row's slots left to
right. Each slot gets the row's best title that isn't on the screen yet: not in the slots of a row above, and not
earlier in this row.
- A title skipped for the screen isn't lost. After the
widthslots, the rest of the row follows in its original order, minus the titles this row already shows, so no title appears twice in one row. - Titles past the
widthslots are off the screen, so they never block a row below. - When no title fits a slot, the row ends there, with nothing after it. Every title it has left is already on the
screen. A row shorter than
widthsimply ends, and an empty row stays empty.
With width = 2, [["dark", "ozark", "lupin"], ["ozark", "dark", "narcos", "you"]] gives
[["dark", "ozark", "lupin"], ["narcos", "you", "ozark", "dark"]].
Level 2. width can also be a list with one width per row. A width of 0 puts nothing of that row on the
screen: the row only drops its own repeats, and it blocks nothing below.
Level 3. exempt is a set of row indexes for rows that skip the global dedup, such as a row of shows the
viewer is partway through. An exempt row ignores the rows above it, still never shows a title twice, and its titles
don't count as on the screen for the rows below.
Level 4. rule="grid" switches to the rule in q-nfx-063: no repeated title in any row or any column of the
first screen. Column j is slot j of every row. A title may now be on the screen twice, in different rows and
different columns. Each slot gets the row's best title that isn't already in this row and isn't in slot j of a row
above. Scan the whole row for each slot, so a title skipped for one column can fill the next. When no title fits a
slot, the row ends there, as in level 1. Widths work as in level 2. An exempt row skips the column check and adds
nothing to the columns. The default, rule="screen", is levels 1 to 3.
Greedy by rank can leave a slot empty that another order would fill. That's the rule here, and the tests expect it. If you finish early, say how you'd fill the most slots instead, what it costs, and what it does to each row's order.