KatInterview

Day-7 User Activity Counter

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Datadata-analysisdate-manipulationset-operations

Last 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 user
  • signup_date: when the user registered (date or timestamp)
  • plan_type: subscription tier

events table with columns:

  • user_id: identifier linking the event to a user
  • event_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_date and 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_id values in users are unique
  • signup_date is an ISO 8601 string (date or datetime)
  • event_ts is an ISO 8601 datetime string
  • Timestamps may include Z suffix for UTC
  • plan_type is 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.