Netflix questions

Viewers With Matching Recent Watches

NetflixPhone screenMedium

You are given a ViewingHistory class built from histories, a dictionary mapping each user id to that user's watch history: movie ids in the order the user watched them, oldest first. A movie may appear more than once in one history.

A user's recent movies for k are the distinct movies among the last k entries of their history. A shorter history uses all its entries, and an empty history gives the empty set. Order and repeats do not matter.

Histories never change after construction.

Part 1: Group users by recent movies

Implement groups(k). It returns every group of two or more users whose recent movies for k are exactly the same set.

Each group lists its user ids in ascending order, and the groups are sorted in ascending order (compared as lists). Users that match nobody are left out.

  1. Example 1
    init
    [{"cy":[2,4,3,7],"ann":[1,2,3,4],"bob":[9,3,4,2],"dee":[5,6],"eve":[6,5],"fay":[4,4,3,2],"gus":[8]}]
    operations
    [["groups",[3]],["groups",[2]],["groups",[1]],["groups",[10]]]
    Output
    [[["ann","bob","fay"],["dee","eve"]],[["dee","eve"]],[["bob","fay"]],[["dee","eve"]]]

    Why: With k = 3, ann, bob and fay all have the recent set {2, 3, 4} despite different orders and a repeat, and dee and eve both have {5, 6}; gus matches nobody. For k = 2 only dee and eve still match, for k = 1 only bob and fay share the movie 2, and for k = 10 the full histories again match only dee and eve.

Constraints

  • 0 <= len(histories) <= 10^4
  • 1 <= k <= 10^4
  • 0 <= len(histories[u]) <= 10^4 for each user u, with at most 10^5 entries in total
  • 1 <= histories[u][i] <= 10^9
  • Each user id u in histories is non-empty and contains only characters with 'a' <= c <= 'z' or '0' <= c <= '9'
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question