You're building analytics for a social media platform to track daily active users. Given an Activity table that records every user interaction, you need to calculate the number of unique active users for each day in a specific 30-day period.
Table Schema:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | session_id | int | | activity_date | date | | activity_type | enum | +---------------+---------+
The activity_type can be: 'open_session', 'end_session', 'scroll_down', or 'send_message'. Any of these activities counts as the user being active on that day.
Goal: Find the daily active user count for the 30-day period ending on 2019-07-27 (inclusive). This means counting unique users who performed at least one activity each day between 2019-06-28 and 2019-07-27.
Output Format: Return a table with day and active_users columns showing the count of unique active users for each day in the specified range.
Input & Output
Visualization
Time & Space Complexity
Database uses efficient sorting and hashing algorithms for GROUP BY and COUNT DISTINCT
Space for d distinct dates and u unique users in the result set
Constraints
- 1 โค number of activity records โค 104
- Each session belongs to exactly one user
- Date range: 30 days ending 2019-07-27 inclusive
- activity_type is one of: 'open_session', 'end_session', 'scroll_down', 'send_message'
- All activity types count as valid user activity