Design a Real-Time Tracker for Live Viewers and the Most-Watched Title
Company: Netflix
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a real-time tracking service that monitors which users are currently watching which titles. The service should support users starting and stopping watching sessions, querying the number of live viewers for a given title, and retrieving the most popular title currently being watched.
Each user can only watch one title at a time. If a user starts watching a new title while already watching another, they automatically stop watching the previous title. Stopping a title the user is not currently watching has no effect.
When multiple titles are tied for the most viewers, return the lexicographically smallest title. If no one is watching anything, return an empty string.
Implement a class `WatchingTracker` with the following methods:
- `startWatching(userId: int, titleId: str)`: Record that the given user has started watching the given title. If the user was already watching a different title, they automatically stop watching it first. If the user is already watching this same title, this is a no-op.
- `stopWatching(userId: int, titleId: str)`: Record that the given user has stopped watching the given title. If the user is not currently watching this title, this has no effect.
- `getLiveViewers(titleId: str) -> int`: Return the number of users currently watching the given title.
- `getMostWatchedTitle() -> str`: Return the title with the most live viewers. If there is a tie, return the lexicographically smallest title. If no one is watching anything, return an empty string.
Example 1 shows a tie and a user switching titles:
```text
Input:
["WatchingTracker", "startWatching", "startWatching", "startWatching", "getLiveViewers", "getMostWatchedTitle", "startWatching", "getMostWatchedTitle", "startWatching", "getLiveViewers", "getLiveViewers"]
[[], [1, "drama1"], [2, "drama1"], [3, "comedy2"], ["drama1"], [], [4, "comedy2"], [], [2, "comedy2"], ["drama1"], ["comedy2"]]
Output: [null, null, null, null, 2, "drama1", null, "comedy2", null, 1, 3]
Explanation:
tracker = WatchingTracker()
tracker.startWatching(1, "drama1") # drama1: {1}
tracker.startWatching(2, "drama1") # drama1: {1, 2}
tracker.startWatching(3, "comedy2") # comedy2: {3}
tracker.getLiveViewers("drama1") # 2
tracker.getMostWatchedTitle() # "drama1" (2 viewers against 1)
tracker.startWatching(4, "comedy2") # comedy2: {3, 4}, now tied with drama1 at 2
tracker.getMostWatchedTitle() # "comedy2", the smaller title in the tie
tracker.startWatching(2, "comedy2") # user 2 leaves drama1: drama1 {1}, comedy2 {2, 3, 4}
tracker.getLiveViewers("drama1") # 1
tracker.getLiveViewers("comedy2") # 3
```
Example 2 shows the no-op cases and the empty result:
```text
Input:
["WatchingTracker", "getMostWatchedTitle", "startWatching", "stopWatching", "getLiveViewers", "startWatching", "getLiveViewers", "stopWatching", "getLiveViewers", "getMostWatchedTitle"]
[[], [], [5, "doc3"], [5, "drama1"], ["doc3"], [5, "doc3"], ["doc3"], [5, "doc3"], ["doc3"], []]
Output: [null, "", null, null, 1, null, 1, null, 0, ""]
Explanation:
tracker = WatchingTracker()
tracker.getMostWatchedTitle() # "" (nobody is watching)
tracker.startWatching(5, "doc3") # doc3: {5}
tracker.stopWatching(5, "drama1") # no effect, user 5 is watching doc3
tracker.getLiveViewers("doc3") # 1
tracker.startWatching(5, "doc3") # no-op, already watching doc3
tracker.getLiveViewers("doc3") # 1
tracker.stopWatching(5, "doc3") # doc3 has no viewers left
tracker.getLiveViewers("doc3") # 0
tracker.getMostWatchedTitle() # ""
```
```hint Lookups in both directions
List the lookups each method needs. A switch has to find the user's previous title, and a viewer count has to find a title's audience. Then check that every write keeps both views in agreement.
```
```hint Titles with no viewers
Decide what happens to a title when its last viewer leaves, and check that the most-watched query and the empty-string case still behave. If the most-watched query is called far more often than users switch, think about keeping titles ordered as their counts change.
```
### Constraints & Assumptions
- `1 <= userId <= 10^5`
- `1 <= titleId.length <= 100`
- `titleId` consists of lowercase English letters and digits.
- At most `10^5` total calls are made across all methods.
- Each user can watch at most one title at a time.
### Clarifying Questions to Ask
- How often is `getMostWatchedTitle` called compared with start and stop events? That decides whether a scan per query is acceptable.
- What happens when a client disappears without calling `stopWatching`, for example after an app crash? Is that in scope?
- Does "lexicographically smallest" mean plain character-code order, so digits sort before letters?
- Can calls arrive from many threads at once, or can the class assume single-threaded access?
### What a Strong Answer Covers
- The state in both directions (what each user is watching, and who is watching each title or how many), kept consistent on every start, switch and stop.
- Correct handling of the no-op cases: starting the title already being watched, and stopping a title the user is not watching.
- `getMostWatchedTitle` with the smallest-title tie-break and the empty-string case, with titles that lost their last viewer removed or ignored.
- Time and space complexity per method, and a faster structure for the most-watched query when it dominates the call mix.
### Follow-up Questions
- Clients sometimes vanish without a stop call. How would you expire stale sessions with heartbeats, and what does that change in the data structures?
- `getMostWatchedTitle` must stay fast under heavy switching. What structure keeps it at $O(\log t)$ for $t$ titles, and what does each update cost?
- How would you return the top 10 titles instead of the single most-watched one?
- The service must handle tens of millions of concurrent viewers across many machines. How would you partition the state, and how exact must the live counts be?
Overview: Implement a tracker that records which title each user is watching, counts live viewers per title and returns the most-watched title with a lexicographic tie-break. It tests keeping two-way mappings consistent on switches and stops, and choosing a structure for fast ranking queries.