496. Design Most Recently Watched Tab for Video Service
Asked in
Design Most Recently Watched Tab for Video Service

Build the recently watched tab of a video streaming service. Each user has their own tab. Whenever a user watches a video, that video becomes the first entry in their tab.

A video appears at most once in a user's tab. Watching it again moves it to the front.

Methods

Constructor

RecentlyWatchedTab()
  • Creates the service with an empty tab for every user.

Watch a Video

void watchVideo(String userId, String videoId)
  • Records that userId watched videoId.
  • The video becomes the first (most recent) entry in that user's tab.
  • If the video is already in the user's tab, its old entry is removed so that it appears only once.

Get Recently Watched Videos

List<String> getRecentlyWatched(String userId, int limit)
  • Returns up to limit video ids from the user's tab, with the most recently watched first.
  • If the user has fewer than limit videos, returns all of them.
  • Returns an empty list if the user's tab is empty.

Remove a Video

boolean removeVideo(String userId, String videoId)
  • If videoId is in the user's tab, removes it and returns true. The other videos keep their order.
  • Otherwise, changes nothing and returns false.
  • If the user watches a removed video again, it goes back in at the front.

Constraints

  • 1 ≤ userId.length, videoId.length ≤ 20
  • userId and videoId contain only lowercase English letters and digits.
  • 1 ≤ limit ≤ 100
  • At most 100,000 calls in total are made to all methods.

Examples

Example 1

  • RecentlyWatchedTab tab = new RecentlyWatchedTab()
  • tab.watchVideo(userId = "alice", videoId = "movie7")
    alice's tab: movie7
  • tab.watchVideo(userId = "alice", videoId = "show2")
    alice's tab: show2, movie7
  • tab.watchVideo(userId = "alice", videoId = "doc5")
    alice's tab: doc5, show2, movie7
  • tab.getRecentlyWatched(userId = "alice", limit = 5) returns ["doc5", "show2", "movie7"]
    Only 3 videos exist, so all are returned.
  • tab.watchVideo(userId = "alice", videoId = "movie7")
    movie7 moves to the front. alice's tab: movie7, doc5, show2
  • tab.getRecentlyWatched(userId = "alice", limit = 2) returns ["movie7", "doc5"]

Example 2

  • RecentlyWatchedTab tab = new RecentlyWatchedTab()
  • tab.watchVideo(userId = "bob", videoId = "a1")
    bob's tab: a1
  • tab.watchVideo(userId = "bob", videoId = "b2")
    bob's tab: b2, a1
  • tab.watchVideo(userId = "carl", videoId = "a1")
    carl's tab: a1. bob's tab is unchanged.
  • tab.removeVideo(userId = "bob", videoId = "a1") returns true
    bob's tab: b2
  • tab.removeVideo(userId = "bob", videoId = "z9") returns false
    z9 is not in bob's tab.
  • tab.getRecentlyWatched(userId = "bob", limit = 3) returns ["b2"]
  • tab.getRecentlyWatched(userId = "carl", limit = 3) returns ["a1"]
  • tab.getRecentlyWatched(userId = "dave", limit = 3) returns []
    dave has not watched anything.
  • tab.watchVideo(userId = "bob", videoId = "a1")
    a1 goes back in at the front. bob's tab: a1, b2
  • tab.getRecentlyWatched(userId = "bob", limit = 3) returns ["a1", "b2"]


Please use Laptop/Desktop or any other large screen to add/edit code.