Customers With More Than 5 Orders (PySpark)

PYSPARK coding challenge · Difficulty: easy · Topic: DataFrame Operations · +50 XP

Problem

Using the PySpark DataFrame API, find the customers who have placed more than 5 orders (the DataFrame equivalent of SQL HAVING COUNT(*) > 5).

Example Input - `sales`

| order_id | customer_id | product_id | amount | order_date |
| --- | --- | --- | --- | --- |
| 1 | 1 | 11 | 100 | 2024-01-01 |
| 2 | 1 | 11 | 100 | 2024-01-02 |
| 3 | 1 | 11 | 100 | 2024-01-03 |
| 4 | 1 | 11 | 100 | 2024-01-04 |
| 5 | 1 | 11 | 100 | 2024-01-05 |
| 6 | 1 | 11 | 100 | 2024-01-06 |
| 7 | 2 | 12 | 200 | 2024-01-01 |
| 8 | 2 | 12 | 200 | 2024-01-02 |
| 9 | 2 | 12 | 200 | 2024-01-03 |
| 10 | 2 | 12 | 200 | 2024-01-04 |
| 11 | 2 | 12 | 200 | 2024-01-05 |
| 12 | 2 | 12 | 200 | 2024-01-06 |
| 13 | 2 | 12 | 200 | 2024-01-07 |
| 14 | 3 | 13 | 300 | 2024-01-01 |
| 15 | 3 | 13 | 300 | 2024-01-02 |
| 16 | 3 | 13 | 300 | 2024-01-03 |

Expected Output

| customer_id |
| --- |
| 1 |
| 2 |

Notes

What this PYSPARK challenge teaches you

“Customers With More Than 5 Orders (PySpark)” is a easy-level PYSPARK challenge focused on DataFrame Operations. Working through it gives you hands-on practice with groupBy, count, filter, HAVING — 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. Count per customer first; the 'more than 5' test applies to that count, not to the raw rows.
  2. groupBy('customer_id').agg(F.count('*').alias('order_count')), then .filter(F.col('order_count') > 5).

Where this comes up

Variations of this problem have been reported in interviews at Flipkart, Amazon, TCS. 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

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 DataFrame Operations.

Solve this challenge free on PySpark.in