New Users Daily Count - Problem
New Users Daily Count
You're working as a data analyst for a social platform and need to track first-time user logins within the last 90 days.
Given a
The
Your task: For every date within the last 90 days from today (2019-06-30), count how many users logged in for the very first time on that date.
๐ฏ Key Challenge: A user might have multiple login records, but you only care about their first login date.
Return: A report showing
You're working as a data analyst for a social platform and need to track first-time user logins within the last 90 days.
Given a
Traffic table that logs user activities:| Column Name | Type |
|---|---|
| user_id | int |
| activity | enum |
| activity_date | date |
The
activity column contains: ('login', 'logout', 'jobs', 'groups', 'homepage')Your task: For every date within the last 90 days from today (2019-06-30), count how many users logged in for the very first time on that date.
๐ฏ Key Challenge: A user might have multiple login records, but you only care about their first login date.
Return: A report showing
login_date and user_count for new users on each date within the 90-day window. Input & Output
example_1.sql โ Basic Example
$
Input:
Traffic table:
| user_id | activity | activity_date |
|---------|----------|---------------|
| 1 | login | 2019-05-01 |
| 1 | homepage | 2019-05-01 |
| 1 | logout | 2019-05-01 |
| 2 | login | 2019-06-21 |
| 2 | logout | 2019-06-21 |
| 3 | login | 2019-01-01 |
| 3 | login | 2019-06-21 |
| 3 | logout | 2019-06-21 |
โบ
Output:
| login_date | user_count |
|------------|------------|
| 2019-05-01 | 1 |
| 2019-06-21 | 1 |
๐ก Note:
User 1's first login: 2019-05-01 (within 90 days). User 2's first login: 2019-06-21 (within 90 days). User 3's first login: 2019-01-01 (outside 90-day window, excluded). Only dates with new users are shown.
example_2.sql โ Multiple Users Same Date
$
Input:
Traffic table:
| user_id | activity | activity_date |
|---------|----------|---------------|
| 1 | login | 2019-06-30 |
| 2 | login | 2019-06-30 |
| 3 | login | 2019-06-30 |
| 4 | login | 2019-06-29 |
| 4 | login | 2019-06-30 |
โบ
Output:
| login_date | user_count |
|------------|------------|
| 2019-06-29 | 1 |
| 2019-06-30 | 3 |
๐ก Note:
Three users (1, 2, 3) had their first login on 2019-06-30. User 4's first login was 2019-06-29 (their login on 2019-06-30 doesn't count as it's not their first). Result shows count of new users per date.
example_3.sql โ Edge Case - No New Users
$
Input:
Traffic table:
| user_id | activity | activity_date |
|---------|----------|---------------|
| 1 | login | 2019-01-01 |
| 1 | login | 2019-06-30 |
| 2 | homepage | 2019-06-30 |
| 2 | logout | 2019-06-29 |
โบ
Output:
(Empty result set)
๐ก Note:
User 1's first login was 2019-01-01 (outside 90-day window). User 2 never logged in (only homepage and logout activities). No users had their first login within the specified date range, so result is empty.
Visualization
Tap to expand
Understanding the Visualization
1
Collect All Transactions
Gather all customer card scans (like login activities in our Traffic table)
2
Find First Visit Date
For each customer, identify their very first visit (minimum date)
3
Filter Recent Period
Keep only first visits within the last 90 days
4
Count New Customers Daily
Group by date and count how many customers joined each day
Key Takeaway
๐ฏ Key Insight: Use GROUP BY with MIN() to find first login dates efficiently, then filter by date range - much faster than checking each date individually!
Time & Space Complexity
Time Complexity
O(n log n)
Single scan of n records plus sorting for window function
โก Linearithmic
Space Complexity
O(n)
Space for storing intermediate results and window function processing
โก Linearithmic Space
Constraints
- 1 โค Traffic table rows โค 105
- user_id is a positive integer
- activity โ {'login', 'logout', 'jobs', 'groups', 'homepage'}
- Date range: exactly 90 days from 2019-04-02 to 2019-06-30
- Multiple rows per user are allowed (duplicate activities on same/different dates)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code