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 Traffic table that logs user activities:

Column NameType
user_idint
activityenum
activity_datedate

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
โ˜• Coffee Shop New Customer Tracker๐Ÿ“Š Raw DataCustomer card scansAlice: May 1, May 5Bob: Jun 21, Jun 25Carol: Jan 1, Jun 21๐Ÿ” First VisitsFind earliest dateAlice: May 1 โœ“Bob: Jun 21 โœ“Carol: Jan 1 โŒ (old)๐Ÿ“… 90-Day FilterApr 2 - Jun 30, 2019Alice: May 1 โœ“Bob: Jun 21 โœ“Carol: Excluded๐Ÿ“ˆ Daily CountsMay 1: 1 new customerJun 21: 1 new customerโ˜•โ˜•โ˜•Each coffee cup = one new customer joining that daySQL Translation:1. WHERE activity = 'login'2. MIN(activity_date) per user3. Filter 90-day window4. COUNT(*) GROUP BY date
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for storing intermediate results and window function processing

n
2n
โšก 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)
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
41.2K Views
High Frequency
~15 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