Count Active Users in Trailing Time Window
Heaps, Sorting & SearchAuto-graded · 24 test casesLast updated
Tests binary search on sorted lists to efficiently count distinct entities with at least one event in a sliding time range.
You are given activity logs for multiple users. Each user has a chronologically ordered list of event timestamps (in seconds). Your task is to determine how many distinct users had at least one event during a specific trailing time window.
Write a function that takes:
events: A list whereevents[i]contains all event timestamps for useri, sorted in ascending orderquery_time: A timestamp in seconds
Return the count of distinct users who had at least one event in the inclusive time range [query_time - 86400, query_time] (the 24-hour period ending at query_time).
Notes:
- 86400 seconds equals 24 hours
- Both endpoints of the time window are inclusive
- Events occurring after
query_timeshould be ignored - Each user contributes at most 1 to the count, regardless of how many events they had in the window
- Event lists may be empty
Constraints
0 <= len(events) <= 10^5(number of users)0 <= len(events[i]) <= 10^5(events per user)0 <= events[i][j] <= 10^9(timestamp values)events[i]is sorted in ascending order0 <= query_time <= 10^9
Examples
Example 1
Input: count_active_users([[100, 200, 300], [150, 250], [400]], 300)
Output: 2
Example 2
Input: count_active_users([[0, 86400], [1, 86401], [500]], 86400)
Output: 3
Example 3
Input: count_active_users([], 100000)
Output: 0
Example 4
Input: count_active_users([[10, 20], [20, 30], [30]], 30)
Output: 3
Hints
Hint 1
For each user, you need to check if any timestamp falls within the window [query_time - 86400, query_time]. Since the timestamps are sorted, can you do better than scanning all of them?
Hint 2
Use binary search to find the boundaries of the valid range. bisect_left and bisect_right from Python's bisect module can help locate the first and last valid timestamps efficiently.
Hint 3
If bisect_left(timestamps, window_start) gives index L and bisect_right(timestamps, query_time) gives index R, then the user was active if L < R. Why does this work?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
For each user, we need to determine whether they have at least one event timestamp within the inclusive range [query_time - 86400, query_time]. Since each user's event list is sorted, we can use binary search instead of scanning all timestamps.
The strategy is:
- Calculate the window start:
window_start = query_time - 86400 - For each user's sorted event list, use binary search to find:
- The leftmost position where an event could be
>= window_startusingbisect_left - The rightmost position where an event could be
<= query_timeusingbisect_right
- The leftmost position where an event could be
- If these positions define a non-empty range, the user was active during the window
Why this works:
bisect_left(timestamps, window_start) returns the index of the first timestamp that is greater than or equal to window_start. Call this index L.
bisect_right(timestamps, query_time) returns the index one position past the last timestamp that is less than or equal to query_time. Call this index R.
The slice timestamps[L:R] contains exactly the timestamps in our target range. If L < R, at least one such timestamp exists, so the user was active. The difference R - L would give the count of events in the window, but we only need to know if any exist.
This approach correctly handles:
- Events exactly at the boundaries (both are inclusive due to our choice of
bisect_leftandbisect_right) - Empty event lists (both indices return 0, so
L < Ris false) - Events entirely before or after the window (the range becomes empty)
Complexity:
- Time: O(U · log K) where U is the number of users and K is the maximum events per user. Each user requires two binary searches, each taking O(log K).
- Space: O(1) additional space beyond the input (we only use a counter and loop variables)
This is much more efficient than the naive O(N) approach where N is the total number of events across all users, especially when individual users have many events.
from bisect import bisect_left, bisect_right
from typing import List
def count_active_users(events: List[List[int]], query_time: int) -> int:
window_start = query_time - 86400
active_count = 0
for user_events in events:
left_idx = bisect_left(user_events, window_start)
right_idx = bisect_right(user_events, query_time)
if left_idx < right_idx:
active_count += 1
return active_count