Cumulative Active Users Over Time (PySpark)
PYSPARK coding challenge · Difficulty: medium · Topic: Aggregation · +100 XP
Problem
For each date, count how many DISTINCT customers have ever been active up to and including that date. This is the 'total users so far' line on a growth chart, so it never goes down.
A customer active on several dates is counted once, on their first appearance — so the number only rises when someone NEW shows up. Between 2024-01-01 and 2024-01-15 it stays flat, because 2024-01-15 was customer 1 returning.
One row per distinct date.
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 | cumulative_users | | --- | --- | | 2023-06-01 | 1 | | 2024-01-01 | 3 | | 2024-01-15 | 3 | | 2024-01-20 | 4 | | 2024-02-05 | 4 | | 2024-02-10 | 5 |
Explanation
It differs from a rolling window in that nothing ever leaves: customer 5 was active once in June 2023 and still counts in February 2024.
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: [Cumulative Active Users Over Time](/challenges/cumulative-active-users-over-time)
What this PYSPARK challenge teaches you
“Cumulative Active Users Over Time (PySpark)” is a medium-level PYSPARK challenge focused on Aggregation. Working through it gives you hands-on practice with Window, collect_set, Cumulative, Aggregation — 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
- Cumulative
- Aggregation
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:
- rowsBetween(Window.unboundedPreceding, 0) keeps everything seen so far.
- F.size(F.collect_set('customer_id').over(w)) counts distinct customers.
- Two rows share 2024-01-01, so take the max per date to get one row each.
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 medium and covers Aggregation.