Monthly Customer Churn Count (PySpark)
PYSPARK coding challenge · Difficulty: hard · Topic: Aggregation · +150 XP
Problem
A customer has churned in a month if they were active the month before and are absent this month. Count the churned customers per month.
Schema — `monthly_active`
| Column | | --- | | customer_id | | month_num |
Example Input — `monthly_active`
| customer_id | month_num | | --- | --- | | 1 | 1 | | 2 | 1 | | 3 | 1 | | 5 | 1 | | 1 | 2 | | 2 | 2 | | 4 | 2 | | 1 | 3 | | 4 | 3 |
Expected Output
| month_num | churned_customers | | --- | --- | | 2 | 2 | | 3 | 1 |
Explanation
Worked through: month 1 had customers 1, 2, 3 and 5. Month 2 has 1, 2 and 4 — so 3 and 5 churned, a count of 2. Month 3 has 1 and 4, so customer 2 churned, a count of 1. Customer 4 is new in month 2, which is growth, not churn.
Month 1 has no month before it, so it produces no row at all. The last month produces no row either — there is no data yet for the month after it, and counting everyone as churned there is the classic off-by-one in this question.
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: [Monthly Customer Churn Count](/challenges/monthly-customer-churn-count)
What this PYSPARK challenge teaches you
“Monthly Customer Churn Count (PySpark)” is a hard-level PYSPARK challenge focused on Aggregation. Working through it gives you hands-on practice with left_anti, join, Churn, 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
- left_anti
- join
- Churn
- 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:
- Shift a copy back a month, then left_anti join on customer_id and month_num.
- left_anti keeps rows from the left side with NO match on the right — exactly 'did not return'.
- Exclude the final month, or everyone active in it looks churned.
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.