Friend groups by matching last k movies
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Netflix's coding / problem solving round, last asked Jan 2026. It tests hashing, grouping and sliding window.
A streaming service stores each customer's full viewing history, oldest title first. Two customers are friends when the set of their last k watched titles is the same. Order inside that window does not matter, and a title watched twice in the window counts once.
Implement:
def friend_groups(customer_ids: list[str], movies: list[list[str]], k: int) -> list[list[str]]
-
customer_idsholds distinct IDs;movies[i]is the history ofcustomer_ids[i]. - Return every friend group with at least two customers. Sort the IDs inside each group, and sort the groups by their first ID.
- A customer who has watched fewer than
ktitles has no full window and belongs to no group.
Constraints: 1 <= k <= 100, up to 10^5 customers, histories up to 10^4 titles each.
Example 1
customer_ids = ["u1", "u2", "u3", "u4"]
movies = [
["Heat", "Alien", "Up", "Jaws"],
["Jaws", "Up", "Alien"],
["Alien", "Jaws", "Up"],
["Jaws", "Heat", "Up"],
]
k = 3 -> [["u1", "u2", "u3"]]
u1, u2 and u3 all end with the set {Alien, Jaws, Up}. u4 ends with {Jaws, Heat, Up}.
Example 2
Same input with k = 1 returns [["u3", "u4"]]: both last watched Up.
Be ready for follow-ups: friends who share at least m of the last k titles, histories that grow as events stream in, and data too large for one machine.
Clarifying questions
- Is the window the last
kentries, even if fewer thankdistinct titles are in it? - Are titles case-sensitive strings?
- If several groups exist, is one customer ever in two groups? (No: each customer has one window set.)
What a strong answer covers
- Builds, for each customer with at least
kentries, thefrozensetof their lastktitles, O(k) each. - Groups customers by that set in a hash map, keeps groups of two or more, sorts IDs inside each and then the groups by first ID.
- Runs in O(customers * k) plus sorting, and never reads a whole history.
- Handles repeated titles in the window, customers with short histories, and
k = 1. - Tests both examples, a window with a repeated title, and no groups at all.
Follow-ups
- Friends share at least
mof their lastktitles (no longer an equivalence; similarity search, MinHash). - Histories grow as events stream in: keep a sliding window and a count per title per customer, updating each customer's group key.
- The data is too large for one machine: partition customers by a hash of their window set.
Where it's asked
- Netflix · Coding / problem solving · Last asked Jan 2026Live coding on under-specified requirements
What it tests
hashing · grouping · sliding window
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
