Analyze Product Sales by Region (PySpark)

PYSPARK coding challenge · Difficulty: easy · Topic: GroupBy Aggregations · +50 XP

Problem

You are given a DataFrame named sales_data that contains product sales across different regions.

Write a PySpark transformation that returns one summary row per product.

For every product, return:

Order the result by product in ascending order.

DataFrame: sales_data

| Column | Type | Description |
|---|---|---|
| `order_id` | INT | Unique order identifier |
| `product` | STRING | Product name |
| `region` | STRING | Region where the sale happened |
| `quantity` | INT | Quantity sold |

Example Input

| order_id | product | region | quantity |
|---|---|---|---|
| 1 | Laptop | East | 2 |
| 2 | Mobile | West | 5 |
| 3 | Laptop | East | 1 |
| 4 | Tablet | North | 3 |
| 5 | Mobile | West | 2 |
| 6 | Laptop | West | 4 |

Expected Output

| product | total_qty | west_qty | region_count | order_count |
|---|---|---|---|---|
| Laptop | 7 | 4 | 2 | 3 |
| Mobile | 7 | 7 | 1 | 2 |
| Tablet | 3 | 0 | 1 | 1 |

Explanation

For Laptop, total quantity is 2 + 1 + 4 = 7, West quantity is 4, it was sold in East and West, and it has 3 orders.

For Mobile, total quantity is 5 + 2 = 7, all sales are from West, it was sold in one region, and it has 2 orders.

For Tablet, total quantity is 3, there is no West sale so west_qty = 0, it was sold in one region, and it has 1 order.

Constraints

Concepts Tested

What this PYSPARK challenge teaches you

“Analyze Product Sales by Region (PySpark)” is a easy-level PYSPARK challenge focused on GroupBy Aggregations. Working through it gives you hands-on practice with groupBy, agg, sum, countDistinct, when, PySpark — 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. Use sales_data.groupBy('product').agg(...).
  2. Use F.when(F.col('region') == 'West', F.col('quantity')).otherwise(0) inside F.sum.
  3. Use F.countDistinct('region') and F.count('*') for the two counts.

Where this comes up

Variations of this problem have been reported in interviews at Amazon, Flipkart, Walmart, Target. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

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

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 easy and covers GroupBy Aggregations.

Solve this challenge free on PySpark.in