Day-7 User Activity Counter
SQL & DataAuto-graded · 16 test casesLast updated
Identify users who recorded at least one activity exactly seven calendar days after joining.
You are analyzing user engagement patterns from two data sources:
users table with columns:
user_id: unique identifier for each usersignup_date: when the user registered (date or timestamp)plan_type: subscription tier
events table with columns:
user_id: identifier linking the event to a userevent_ts: when the event occurred (timestamp)
Your task is to determine how many distinct users logged at least one event on the calendar date that falls exactly seven days after their registration date.
Specifically:
- Extract the calendar date from each user's
signup_dateand add 7 days to find their target date - Extract the calendar date from each event's
event_ts - A user qualifies if any of their events matches their target date (ignore time-of-day)
- Count each qualifying user once, regardless of how many events they had on that date
- Users with events on day 6, day 8, or any other day do not qualify
- Events from unknown user IDs (not in the users table) should be ignored
Return the count as an integer. If no users qualify, return 0.
Constraints
- 0 ≤ len(users) ≤ 10⁵
- 0 ≤ len(events) ≤ 10⁶
- All
user_idvalues in users are unique signup_dateis an ISO 8601 string (date or datetime)event_tsis an ISO 8601 datetime string- Timestamps may include
Zsuffix for UTC plan_typeis a non-empty string
Examples
Example 1
Input: count_day_seven_active_users([{'user_id': 'u1', 'signup_date': '2024-01-01T10:30:00', 'plan_type': 'free'}, {'user_id': 'u2', 'signup_date': '2024-01-05', 'plan_type': 'pro'}, {'user_id': 'u3', 'signup_date': '2024-01-10T23:00:00', 'plan_type': 'free'}], [{'user_id': 'u1', 'event_ts': '2024-01-08T00:05:00'}, {'user_id': 'u1', 'event_ts': '2024-01-08T15:00:00'}, {'user_id': 'u2', 'event_ts': '2024-01-11T09:00:00'}, {'user_id': 'u3', 'event_ts': '2024-01-17T23:59:59'}])
Output: 2
Example 2
Input: count_day_seven_active_users([{'user_id': 'a', 'signup_date': '2024-02-25', 'plan_type': 'free'}, {'user_id': 'b', 'signup_date': '2024-02-28T18:00:00', 'plan_type': 'pro'}], [{'user_id': 'a', 'event_ts': '2024-03-03T12:00:00'}, {'user_id': 'b', 'event_ts': '2024-03-06T18:00:00'}])
Output: 2
Example 3
Input: count_day_seven_active_users([{'user_id': 'x', 'signup_date': '2024-06-01', 'plan_type': 'free'}], [])
Output: 0
Hints
Hint 1
The problem asks for an exact calendar date match, not a time window. Compare dates after stripping time components.
Hint 2
Build a lookup mapping each user_id to their target date (signup + 7 days), then scan events to find matches.
Hint 3
Use a set to collect matching user IDs so each user is counted only once, even if they have multiple events on day 7.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core challenge is matching events to a precise calendar date for each user, not a time range. Here's the strategy:
-
Compute each user's target date: Parse their signup date/timestamp, extract just the calendar date, and add exactly 7 days. Store this in a dictionary keyed by
user_id. -
Scan events for matches: For each event, look up the user's target date. Parse the event timestamp to extract its calendar date. If the event's date equals the target date, mark that user as active on day 7.
-
Count distinct users: Use a set to collect matching user IDs. This automatically handles users with multiple events on day 7—they appear in the set only once.
-
Handle edge cases: Events from user IDs not present in the users table have no signup date to compare against, so skip them. Empty inputs naturally produce a count of zero.
Why This Works
Comparing calendar dates (rather than computing time intervals) ensures correctness across time-of-day variations. A user who signed up at 11:59 PM and logged an event at 12:01 AM seven calendar days later will match, because both timestamps reduce to dates that are exactly 7 days apart. Computing event_ts - signup_date as a duration would fail this case.
The set data structure guarantees each user is counted once, matching the problem's requirement to count distinct users.
Complexity
- Time: O(U + E) where U is the number of users and E is the number of events. We iterate over each collection once. Dictionary lookups and set insertions are O(1) average case.
- Space: O(U) to store the target date mapping and the result set. In the worst case where every user qualifies, the set size is U.
Full Solution
from datetime import datetime, timedelta
from typing import Dict, List
def count_day_seven_active_users(users: List[Dict[str, str]], events: List[Dict[str, str]]) -> int:
# Build a mapping from user_id to the calendar date 7 days after signup
target_dates = {}
for user in users:
signup_str = user["signup_date"].replace("Z", "+00:00")
signup_date = datetime.fromisoformat(signup_str).date()
target_dates[user["user_id"]] = signup_date + timedelta(days=7)
# Track which users had events on their target date
active_on_day_seven = set()
for event in events:
uid = event["user_id"]
if uid not in target_dates:
continue # skip events from users not in our dataset
event_str = event["event_ts"].replace("Z", "+00:00")
event_date = datetime.fromisoformat(event_str).date()
if event_date == target_dates[uid]:
active_on_day_seven.add(uid)
return len(active_on_day_seven)