Analyze Sales Data to Find Top Performers by Region
PYSPARK coding challenge · Difficulty: hard · Topic: GroupBy Aggregations · +200 XP
Problem
Given a sales_data table that contains information about sales transactions across various regions, your task is to identify the top 2 salespersons per region based on the total sales amount. The table might contain duplicate transactions due to report generation errors. You need to ensure that duplicates are handled correctly and that only the top 2 salespersons by total sales amount are returned for each region.
Your Task
Remove the duplicated transactions, total each salesperson's sales within their region, and return the top two per region.
Example Input - `sales_data`
| region | salesperson | sales_amount | | --- | --- | --- | | North | Alice | 300 | | North | Alice | 300 | | North | Bob | 500 | | North | Charlie | 200 | | South | David | 700 | | South | Eve | 600 | | South | Eve | 600 | | South | Frank | 300 |
Expected Output
| region | salesperson | total_sales | | --- | --- | --- | | North | Bob | 500 | | North | Alice | 300 | | South | David | 700 | | South | Eve | 600 |
Implementation Notes
- The DataFrame is created for you - do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show()
Constraints
- Between 1 and 10,000,000 rows.
- Every region has at least one salesperson.
- sales_amount is a positive integer.
- A region with only one salesperson returns only that one.
Success Criteria
- Your DataFrame has region, salesperson and total_sales.
- Duplicate transactions are removed BEFORE summing, or the totals come out too high.
- Ranking happens within each region, not across the whole dataset.
- At most two salespeople come back per region, the larger total first.
- You finish by calling .show() on the result, which is what the grader reads.
What this PYSPARK challenge teaches you
“Analyze Sales Data to Find Top Performers by Region” is a hard-level PYSPARK challenge focused on GroupBy Aggregations. Working through it gives you hands-on practice with groupBy, window, filter — 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
- groupBy
- window
- filter
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:
- Two separate problems, in order: the duplicates are a data-quality issue and must be dealt with before any total is computed.
- dropDuplicates() removes identical rows. Then group by region and salesperson to get each person's total.
- Ranking inside a group is a window: partitionBy('region').orderBy(F.col('total_sales').desc()), then keep row_number() <= 2.
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
- Calculate Total Sales and Average Order Value by Customer
- Count Rows per City
- Analyze Product Sales by Region (PySpark)
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
- Parse Apache Logs with Regex
Helpful resources
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 GroupBy Aggregations.