Users Active for a Week Straight: Detect 7-Day Streaks in Event Logs
Arrays & HashingAuto-graded · 15 test casesLast updated
Find users whose event log contains at least seven consecutive UTC calendar days of activity, using per-user day deduplication and a sorted streak scan.
Users Active for a Week Straight
You are given a log of view events. Each event is a list [event_id, user_id, timestamp], where:
event_idis a unique integer identifying the event,user_idis an integer identifying the user who generated the event,timestampis a string in ISO 8601 format, for example"2024-03-01 10:15:00". A string may also end inZor carry an explicit UTC offset such as+00:00.
Return the ids of all users who were active on at least seven calendar days in a row.
Rules
- A user is active on a calendar day if they have one or more events on that day. Several events on the same day count as a single active day.
- The calendar day of an event is its date in UTC.
- A timestamp with a trailing
Zor an explicit offset must be converted to UTC before its date is taken. - A timestamp with no timezone information is already in UTC.
- A timestamp with a trailing
- A streak is a run of calendar days where each day is exactly one day after the previous one, with no missing days. Streaks may cross month and year boundaries.
- A streak longer than seven days qualifies the user, just as a streak of exactly seven does.
- Each qualifying user appears once in the result, even if they have several qualifying streaks.
- Return the user ids sorted in ascending order. If no user qualifies, return an empty list.
Constraints
1 <= len(events) <= 10,000,000event_idvalues are unique.user_idis an integer.- Every timestamp string is parseable by Python's
datetime.fromisoformatonce a trailingZis replaced with+00:00.
Examples
Example 1
Input: find_users_with_week_streak([[1, 101, '2024-03-01 10:15:00'], [2, 101, '2024-03-01 18:30:00'], [3, 101, '2024-03-02 09:00:00'], [4, 101, '2024-03-03 14:22:00'], [5, 101, '2024-03-04 11:05:00'], [6, 101, '2024-03-05 16:45:00'], [7, 101, '2024-03-06 20:10:00'], [8, 101, '2024-03-07 08:00:00'], [9, 202, '2024-03-01 12:00:00'], [10, 202, '2024-03-02 12:00:00'], [11, 202, '2024-03-04 12:00:00']])
Output: [101]
Hints
Hint 1
Several events on one day should count once, so first reduce each user's events to a set of distinct UTC dates.
Hint 2
Once a user's dates are sorted and distinct, a streak continues exactly when the gap between neighbouring dates is one day; any other gap resets the streak to 1.
Hint 3
You can stop scanning a user as soon as their streak reaches 7, which also guarantees that each user is reported only once.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Reduce events to active days. Parse each timestamp and convert it to a UTC calendar date. Timestamps with a timezone are converted to UTC. Naive timestamps are treated as UTC already. Store the dates in a set per user, which removes duplicate events on the same day.
- Scan each user's days in order. Sort the user's distinct dates. Walk through them while keeping a running streak length:
- If the current date is exactly one day after the previous one, increase the streak.
- Otherwise reset the streak to 1.
- When the streak reaches 7, record the user and stop scanning that user.
- Sort the result in ascending order and return it.
Why it is correct
- After deduplication, each element of a user's sorted list is a distinct active day, so "neighbouring entries differ by one day" is equivalent to "the days are consecutive".
- The running streak always equals the length of the maximal consecutive run ending at the current date, so it reaches 7 exactly when some run of 7 or more days exists.
- Longer streaks pass through length 7 on the way, so they are detected too.
- Stopping at the first success adds each user at most once, which satisfies the uniqueness rule.
- Date arithmetic with
datetime.datehandles month and year boundaries and leap days correctly.
Complexity
- Time:
O(N + D log D), whereNis the number of events andDis the number of distinct (user, day) pairs. Building the sets is linear, and sorting each user's days dominates the rest. - Space:
O(D)for the per-user day sets.
Reference solution
from datetime import datetime, timezone
from typing import List
def find_users_with_week_streak(events: List[List]) -> List[int]:
# Collect the distinct UTC calendar days for each user.
active_days = {}
for row in events:
user_id, timestamp = row[1], row[2]
parsed = datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
if parsed.tzinfo:
day = parsed.astimezone(timezone.utc).date()
else:
day = parsed.date()
active_days.setdefault(user_id, set()).add(day)
# Scan each user's sorted days for a run of at least 7 consecutive days.
qualifying = []
for user_id, days in active_days.items():
ordered_days = sorted(days)
streak = 1
for index in range(1, len(ordered_days)):
if (ordered_days[index] - ordered_days[index - 1]).days == 1:
streak += 1
if streak >= 7:
qualifying.append(user_id)
break
else:
streak = 1
return sorted(qualifying)