Rolling 30-Day Active Customers (PySpark)
PYSPARK coding challenge · Difficulty: hard · Topic: Aggregation · +150 XP
Problem
For each date on which anything happened, count how many DISTINCT customers were active in the trailing 30 days — that date and the 29 before it.
A customer active twice in the window counts once. On 2024-01-15 the window reaches back to 2023-12-17, catching customers 1 and 2 from 2024-01-01 and customer 1 again on the day itself, so the answer is 2, not 3.
One row per distinct date, not one per activity row.
Schema — `user_activity`
| Column | | --- | | customer_id | | activity_date |
Example Input — `user_activity`
| customer_id | activity_date | | --- | --- | | 5 | 2023-06-01 | | 1 | 2024-01-01 | | 2 | 2024-01-01 | | 1 | 2024-01-15 | | 3 | 2024-01-20 | | 2 | 2024-02-05 | | 4 | 2024-02-10 |
Expected Output
| activity_date | active_customers_30d | | --- | --- | | 2023-06-01 | 1 | | 2024-01-01 | 2 | | 2024-01-15 | 2 | | 2024-01-20 | 3 | | 2024-02-05 | 3 | | 2024-02-10 | 4 |
Explanation
The obvious COUNT(DISTINCT ...) OVER (...) does not work — MySQL does not allow DISTINCT inside a window function. Use a correlated subquery: for each date, count the distinct customers inside its own 30-day window.
Notes
- The DataFrame is created for you — do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show() - Same problem in SQL: [Rolling 30-Day Active Customers](/challenges/rolling-30-day-active-customers)
What this PYSPARK challenge teaches you
“Rolling 30-Day Active Customers (PySpark)” is a hard-level PYSPARK challenge focused on Aggregation. Working through it gives you hands-on practice with Window, collect_set, rangeBetween, Rolling Window — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- Window
- collect_set
- rangeBetween
- Rolling Window
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- rangeBetween needs a numeric column: F.datediff turns a date into a day number.
- F.size(F.collect_set('customer_id').over(w)) is the distinct count.
- Select just the two output columns and .distinct() so each date appears once.
How to practise it on PySpark.in
Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYSPARK challenges
- Find Duplicate Emails
- HR: Average Salary by Department
- Logistics: Count Shipments by Status
- Count Total Orders Placed by Each Customer
- Find Average Order Amount for Each Customer
- Find Customers Who Placed More Than 5 Orders (HAVING)
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated hard and covers Aggregation.