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.

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 |

Notes

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

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:

  1. 1 ≤ number of rows ≤ 10,000,000
  2. Each region contains at least 1 salesperson
  3. sales_amount is a positive integer
  4. Output must have exactly two rows per region unless only one salesperson exists
  5. Handle duplicate transactions correctly before summing

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

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.

Solve this challenge free on PySpark.in