Coding questions · Netflix
Dedupe titles in per-shelf viewport
This question evaluates algorithmic problem-solving and state management for streaming deduplication with order preservation, testing competencies in data structures, deterministic filtering, and handling constrained visibility windows.
You are rendering a streaming app home page.
- The home page consists of shelves displayed from top to bottom.
- Each shelf contains a list of
titleIdvalues (integers). - Each shelf’s horizontal viewport can show at most X titles.
- Assume the vertical viewport is unlimited (i.e., you process shelves in order, top to bottom).
Implement a function that filters titles while preserving order under the following deduplication rules:
- Global dedupe within the visible region:
- While building the first
Xoutput titles for a shelf, you must skip anytitleIdthat has already appeared in the visible region (firstXoutput titles) of any earlier shelf.
- While building the first
- Local dedupe after the visible region:
- After a shelf has already produced
Xoutput titles, continue outputting additional titles from that shelf, but now only dedupe within the same shelf (i.e., skip repeats that have already been output in this shelf). - Global deduplication no longer applies past position
Xwithin a shelf.
- After a shelf has already produced
Function signature
- Input:
shelves: List[List[int]],X: int - Output:
List[List[int]](the filtered shelves)
Notes
- Preserve the original relative order of titles within each shelf.
- If a shelf has fewer than
Xeligible titles (after global dedupe), it may output fewer thanXtitles.
Example
If X = 2 and shelves are:
- Shelf 1:
[1, 2, 1, 3] - Shelf 2:
[2, 4, 2, 5]
Then:
- Shelf 1 visible region (first 2 outputs) becomes
[1, 2](global seen ={1,2}), then locally can output3(skipping the repeated1). - Shelf 2 visible region cannot use
2(already globally visible), so it outputs[4, 2]as its first 2 outputs (4 is new globally visible; once the shelf has hitXoutputs,2is allowed even though it was globally visible). After that it can output5.
So the output is [[1, 2, 3], [4, 2, 5]].
Specify and implement the algorithm.
Dedupe Titles in Per-Shelf Viewport
You are rendering a streaming app home page.
- The home page consists of shelves displayed from top to bottom.
- Each shelf contains a list of integer
titleIdvalues. - Each shelf has a horizontal viewport that can show at most X titles.
- The vertical viewport is unlimited (process shelves in order, top to bottom).
You must filter titles while preserving order using these rules:
Definitions
- For each shelf, the viewport window is the first X positions of the original shelf list (indices
0..X-1).- Important: even if filtering removes items, you do not extend the viewport window to pull in more items for global dedupe purposes.
Deduplication rules
-
Global dedupe inside the viewport window (raw first X positions):
- When processing an item whose original index
i < X, you must skip it if itstitleIdhas already been output from the viewport window of any earlier shelf. - If you do output it from within the viewport window, it becomes globally visible and affects later shelves.
- When processing an item whose original index
-
Local dedupe everywhere (entire shelf):
- Regardless of index, a shelf must never output the same
titleIdtwice.
- Regardless of index, a shelf must never output the same
-
No global dedupe beyond the viewport window:
- For items with original index
i >= X, global dedupe does not apply (but local dedupe still does). This means a title skipped in the viewport window due to global dedupe may still appear later in the same shelf if it appears again beyond the first X positions.
- For items with original index
Return the filtered shelves.
Constraints
- 0 <= X <= 10^5
- 0 <= number of shelves <= 10^5 (practical limits depend on total titles)
- 0 <= total number of title IDs across all shelves <= 2 * 10^5
- titleId values fit in 32-bit signed integers
- Must preserve relative order of remaining titles within each shelf