User Activity for the Past 30 Days I - Problem

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

example_1.sql โ€” Basic Daily Activity
$ Input: Activity table: +--------+------------+---------------+---------------+ | user_id| session_id | activity_date | activity_type | +--------+------------+---------------+---------------+ | 1 | 1 | 2019-07-20 | open_session | | 1 | 1 | 2019-07-20 | scroll_down | | 1 | 1 | 2019-07-20 | end_session | | 2 | 4 | 2019-07-20 | open_session | | 2 | 4 | 2019-07-21 | send_message | | 3 | 2 | 2019-07-21 | open_session | | 3 | 2 | 2019-07-21 | end_session | | 4 | 3 | 2019-06-25 | open_session | +--------+------------+---------------+---------------+
โ€บ Output: +------------+--------------+ | day | active_users | +------------+--------------+ | 2019-07-20 | 2 | | 2019-07-21 | 2 | +------------+--------------+
๐Ÿ’ก Note: For 2019-07-20: Users 1 and 2 were active (user 1 had multiple activities but counts as 1). For 2019-07-21: Users 2 and 3 were active. The activity on 2019-06-25 is outside our 30-day window ending 2019-07-27, so it's excluded.
example_2.sql โ€” Edge Case: Same User Multiple Sessions
$ Input: Activity table: +--------+------------+---------------+---------------+ | user_id| session_id | activity_date | activity_type | +--------+------------+---------------+---------------+ | 1 | 1 | 2019-07-27 | open_session | | 1 | 2 | 2019-07-27 | open_session | | 1 | 1 | 2019-07-27 | send_message | | 1 | 2 | 2019-07-27 | scroll_down | | 2 | 3 | 2019-07-27 | end_session | +--------+------------+---------------+---------------+
โ€บ Output: +------------+--------------+ | day | active_users | +------------+--------------+ | 2019-07-27 | 2 | +------------+--------------+
๐Ÿ’ก Note: Even though user 1 has multiple sessions and activities on 2019-07-27, they count as only 1 unique active user. User 2 also had activity, so total active users = 2.
example_3.sql โ€” Boundary Date Testing
$ Input: Activity table: +--------+------------+---------------+---------------+ | user_id| session_id | activity_date | activity_type | +--------+------------+---------------+---------------+ | 1 | 1 | 2019-06-27 | open_session | | 1 | 1 | 2019-06-28 | open_session | | 2 | 2 | 2019-07-27 | scroll_down | | 3 | 3 | 2019-07-28 | send_message | +--------+------------+---------------+---------------+
โ€บ Output: +------------+--------------+ | day | active_users | +------------+--------------+ | 2019-06-28 | 1 | | 2019-07-27 | 1 | +------------+--------------+
๐Ÿ’ก Note: 2019-06-27 is excluded (31 days before 2019-07-27). 2019-06-28 is the start of our 30-day window (30 days before). 2019-07-27 is included (end date). 2019-07-28 is excluded (after end date).

Visualization

Tap to expand
๐Ÿ“Š Daily Active Users AnalyticsSocial Media Platform - 30 Day User Activity Report๐Ÿ“ Activity LogUser | Date | Action101 | 06-28 | login102 | 06-28 | scroll101 | 06-28 | message103 | 06-29 | login101 | 06-29 | scroll...10,000+ records๐Ÿ—‚๏ธ Group by Date2019-06-28[101,102,104...]2019-06-29[101,103,105...]...30 daily groups๐Ÿ”ข Count Unique06-28: 1,247 users06-29: 1,156 users06-30: 1,089 users...Daily DAU counts๐Ÿ“ˆ Analytics ReportDay | Active Users2019-06-28 | 1,2472019-06-29 | 1,1562019-06-30 | 1,0892019-07-01 | 1,234...2019-07-27 | 1,345Key Insights:โ€ข Peak: 1,456 usersโ€ข Avg: 1,198 users/dayโ€ข 30-day growth: +8%๐ŸŽฏ SQL GROUP BY + COUNT DISTINCT handles millions of records efficiently!Database engines optimize aggregation operations for O(n log n) performance
Understanding the Visualization
1
Filter Time Window
Just like reviewing security footage from the past 30 days, we filter activities between 2019-06-28 and 2019-07-27
2
Group by Day
Organize all visitor interactions by date, like sorting entry logs into daily folders
3
Count Unique Visitors
For each day's folder, count how many different people visited (not how many times they entered)
4
Generate Report
Create a daily summary showing date and unique visitor count for analytics dashboard
Key Takeaway
๐ŸŽฏ Key Insight: Use SQL's built-in aggregation functions (GROUP BY, COUNT DISTINCT) rather than manual counting. Database engines are highly optimized for these operations and can handle large datasets efficiently with internal sorting, hashing, and indexing mechanisms.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Database uses efficient sorting and hashing algorithms for GROUP BY and COUNT DISTINCT

n
2n
โšก Linearithmic
Space Complexity
O(d + u)

Space for d distinct dates and u unique users in the result set

n
2n
โœ“ Linear Space

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
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
89.3K Views
High Frequency
~8 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen