Count Distinct Signed-In Users in Every Window of K Events
Last updated: November 29, 2026
Quick Overview
Given a sequence of user IDs from consecutive sign-in events and an integer k, return the number of distinct users in each contiguous window of k events.
0
0
0 solved
Given a sequence of user IDs from consecutive sign-in events and an integer k, return the number of distinct users in each contiguous window of k events.
Framed around a Google Workspace sign-in monitor that flags bursts of distinct accounts, this checks a fixed-size window backed by a frequency map. The interviewer wants O(1) updates per step and correct removal of users whose count drops to zero.
What the Interviewer Expects
- Maintains a frequency dictionary for the current window
- Deletes keys when their count reaches zero
- Emits a result for each of the n - k + 1 windows
- Handles k larger than the input
- States O(n) time and O(k) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you approximate this with bounded memory for very large windows (for example, HyperLogLog)?
- What if windows are defined by time (last 5 minutes) instead of event count?
- How would you shard this across machines by tenant?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list
users, integerk >= 1. - Output: list of length
n - k + 1where entryiis the number of distinct IDs inusers[i:i+k]. - If
k > n, return an empty list....
High-Level Design
Keep a dictionary from user ID to its count inside the window. Add each new event; once the window has more than k events, decrement the count for the event that just left and delete the key when it...