Top 3 Customers by Region (PySpark)
PYSPARK coding challenge · Difficulty: medium · Topic: Window Functions · +120 XP
Problem
For each region, find the three customers who spent the most in total.
A customer's total is the sum of amount across all of their sales rows.
If a region has fewer than three customers, return all of them.
Schema
customers
| Column | Type | | --- | --- | | customer_id | int | | customer_name | string | | region | string |
sales
| Column | Type | | --- | --- | | sale_id | int | | customer_id | int | | amount | int |
Example Input
customers
| customer_id | customer_name | region | | --- | --- | --- | | 1 | Alice | North | | 2 | Bob | North | | 3 | Carol | North | | 4 | Dan | North | | 5 | Eve | South | | 6 | Frank | South | | 7 | Grace | South | | 8 | Henry | West | | 9 | Irene | West |
sales (14 rows) — Alice 500 + 300, Bob 400 + 250, Carol 900, Dan 200 + 100,
Eve 700 + 150, Frank 600, Grace 450 + 50, Henry 1000, Irene 250
Expected Output
| region | customer_name | total_amount | | --- | --- | --- | | North | Carol | 900 | | North | Alice | 800 | | North | Bob | 650 | | South | Eve | 850 | | South | Frank | 600 | | South | Grace | 500 | | West | Henry | 1000 | | West | Irene | 250 |
Explanation
North has four customers — Carol 900, Alice 800, Bob 650, Dan 300. Only the
top three are returned; Dan is 4th and is dropped. This is the case that a
missing filter gets wrong.
South has exactly three — Eve 850, Frank 600, Grace 500. All qualify.
West has only two — Henry 1000, Irene 250. Both are returned; there is no
third row to invent.
No two customers inside a region share a total, so row_number, rank and
dense_rank all give the same answer here.
Notes
- The DataFrames are created for you — do not recreate them
- Build a DataFrame called
df_resultand finish withdf_result.show() - Output columns must be exactly
region,customer_name,total_amount,
ordered by region ascending then total_amount descending
- Same problem in SQL: [Top 3 Customers by Region](/challenges/top-3-customers-by-region)
What this PYSPARK challenge teaches you
“Top 3 Customers by Region (PySpark)” is a medium-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with Window, row_number, partitionBy, groupBy, Top N — 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
- row_number
- partitionBy
- groupBy
- Top N
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:
- join, then groupBy("region", "customer_name").agg(F.sum("amount").alias("total_amount")).
- Window.partitionBy("region").orderBy(F.col("total_amount").desc()), then F.row_number().over(w) and filter it to <= 3.
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
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
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 Window Functions.