32. New Users per Day
Difficulty: Medium · Topics: Aggregations
From the events log (many events per user per day), count how many users were seen for the first time on each day.
Return day and new_users, only for days with at least one new user.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
events
| user_id | day |
|---|---|
| 1 | 2024-01-01 |
| 2 | 2024-01-01 |
| 1 | 2024-01-02 |
| 3 | 2024-01-02 |
| 1 | 2024-01-01 |
Expected output
| day | new_users |
|---|---|
| 2024-01-01 | 2 |
| 2024-01-02 | 1 |
Hints
Hint 1
First find each user's first day:groupBy("user_id").agg(F.min("day")).Hint 2
Then count users per first day.Hint 3
Days on which only returning users appear simply don't show up.PySpark functions you'll practise
- groupBy
- agg
- count
- min
Related problems
- Consecutive Status Runs (Gaps and Islands) · Hard · Window Functions
- Monthly Retention by Cohort · Hard · Joins
- Average Salary by Department · Easy · Aggregations
- Word Count · Medium · Arrays
- Departments with Large Teams · Easy · Aggregations
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings
Difficulty: Easy · Medium · Hard · PySpark interview roadmap · All problems