Count unique active users in a sliding window of the last N minutes
Company: Google
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a tracker that counts unique active users within a sliding window of the last `N` minutes, fed by a continuous log stream.
The tracker receives log entries, each with a user ID and a timestamp in seconds. It must report how many distinct users were active within the last `N` minutes relative to a query timestamp.
A user is active at `queryTime` if they have at least one log entry with a timestamp strictly greater than `queryTime - N * 60` and less than or equal to `queryTime`.
Implement:
- `ActiveUsersTracker(timeWindow: int)` creates a tracker whose window is `timeWindow` minutes (this is `N`).
- `addLog(userId: int, timestamp: int) -> None` records an activity event for the user at the given timestamp, in seconds.
- `activeUsers(timestamp: int) -> int` returns the number of unique active users in the last `N` minutes as of the given timestamp.
- `getActiveUserIds(timestamp: int) -> List[int]` returns the unique active user IDs as of the given timestamp, sorted ascending.
Example 1:
```text
operations = ["ActiveUsersTracker", "addLog", "addLog", "addLog", "activeUsers", "getActiveUserIds", "activeUsers"]
arguments = [[5], [1, 100], [2, 200], [1, 250], [300], [300], [500]]
output = [null, null, null, null, 2, [1, 2], 1]
```
The window is 5 minutes, or 300 seconds. At `t = 300` it covers `(0, 300]`, so all three logs count and the distinct users are 1 and 2. At `t = 500` it covers `(200, 500]`: the logs at 100 and 200 drop out, and only user 1's log at 250 remains.
Example 2:
```text
operations = ["ActiveUsersTracker", "addLog", "addLog", "addLog", "activeUsers", "addLog", "activeUsers", "getActiveUserIds"]
arguments = [[1], [1, 10], [2, 20], [3, 30], [70], [4, 80], [80], [80]]
output = [null, null, null, null, 2, null, 2, [3, 4]]
```
The window is 60 seconds. At `t = 70` it covers `(10, 70]`, so user 1's log at 10 is excluded and users 2 and 3 count. After user 4 logs at 80, the window at `t = 80` covers `(20, 80]`: user 2's log at 20 is excluded, and users 3 and 4 remain.
```hint What can be thrown away
Ask which log entries can never matter again once the clock passes a certain point, and where in your storage those entries sit.
```
```hint Counting each user once
A user with several logs in the window still counts once. Decide what you need to store so that a user leaves the active set only when their last log inside the window expires.
```
### Constraints & Assumptions
- `1 <= timeWindow <= 10000` (in minutes)
- `1 <= userId <= 10^6`
- Timestamps are non-negative integers in seconds.
- At most `10^5` calls in total to `addLog`, `activeUsers` and `getActiveUserIds`.
### Clarifying Questions to Ask
- Do log entries arrive in timestamp order, and can a query ask about a time earlier than a previous query or log?
- Can a few users send a very large number of logs inside one window, so that memory per log matters?
- Does the tracker run in one process, or do logs come from many servers that need one combined count?
### What a Strong Answer Covers
- A time-ordered store of log entries with eviction from the old end, using the exact boundary from the definition, and the window converted from minutes to seconds.
- Per-user bookkeeping that keeps a user active while any of their entries is still inside the window.
- Amortized cost per call, with each log inserted and removed once, plus the sorting cost of the ID list.
- The ordering assumption stated out loud, and what has to change if logs or queries arrive out of order.
### Follow-up Questions
- How would you cut memory when a few users send thousands of logs per minute?
- How would you answer queries for arbitrary past timestamps?
- How would you count active users across many servers, and what changes if an approximate count is acceptable?
- How would you handle logs that arrive a few seconds late?
Overview: Design a tracker that reads a stream of user activity logs and reports how many distinct users were active in the last N minutes, along with their sorted IDs. It tests sliding-window design with a queue and per-user counts, exact window boundaries, amortized complexity and the ordering assumptions behind eviction.