← Questions

Friend groups by matching last k movies

0
NetflixCodingLast asked Jan 2026

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_ids holds distinct IDs; movies[i] is the history of customer_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 k titles 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 k entries, even if fewer than k distinct 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 k entries, the frozenset of their last k titles, 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 m of their last k titles (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

What it tests

hashing · grouping · sliding window

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding