Amazon: Customers Active on Exactly 3 Days Last Month (PySpark)

PYSPARK coding challenge · Difficulty: easy · Topic: Date Functions · +50 XP

Problem

*Amazon-style interview practice.*

Topic: Date Functions · COUNT(DISTINCT ...) · GROUP BY / HAVING

Amazon's retention team wants to know who shops regularly but not daily. Find every customer who made purchases on exactly three different days last month, and how many purchases they made in that month, with the PySpark DataFrame API (or Spark SQL).

Assign the result to df_result with the columns customer_id and purchase_count, sorted by customer_id, and finish with df_result.show().

Schema

DataFrame: purchases

| Column | Type | Meaning |
| --- | --- | --- |
| purchase_id | integer | Unique id of the purchase |
| customer_id | string | The customer who paid |
| purchase_ts | string | When the purchase was made, `yyyy-MM-dd HH:mm:ss` |
| amount | double | Order value in rupees |

Example Input: `purchases`

| purchase_id | customer_id | purchase_ts | amount |
| --- | --- | --- | --- |
| 1 | C7 | 2025-02-03 10:00:00 | 410.00 |
| 2 | C7 | 2025-02-11 13:30:00 | 95.00 |
| 3 | C7 | 2025-02-25 18:15:00 | 1200.00 |
| 4 | C3 | 2025-02-28 23:30:00 | 1299.00 |
| 5 | C3 | 2025-03-01 00:00:00 | 220.00 |
| 6 | C1 | 2025-03-02 10:15:00 | 499.00 |
| 7 | C1 | 2025-03-02 18:40:00 | 120.00 |
| 8 | C5 | 2025-03-04 16:20:00 | 1599.00 |
| 9 | C2 | 2025-03-05 11:00:00 | 799.00 |
| 10 | C4 | 2025-03-08 09:00:00 | 300.00 |
| 11 | C4 | 2025-03-08 20:00:00 | 450.00 |
| 12 | C7 | 2025-03-09 11:45:00 | 330.00 |
| 13 | C1 | 2025-03-10 09:05:00 | 2499.00 |
| 14 | C2 | 2025-03-12 19:30:00 | 150.00 |
| 15 | C3 | 2025-03-15 12:00:00 | 640.00 |
| 16 | C5 | 2025-03-18 10:10:00 | 210.00 |
| 17 | C4 | 2025-03-19 14:25:00 | 89.00 |
| 18 | C2 | 2025-03-20 08:45:00 | 999.00 |
| 19 | C5 | 2025-03-25 21:00:00 | 560.00 |
| 20 | C2 | 2025-03-28 22:10:00 | 75.00 |
| 21 | C1 | 2025-03-31 21:45:00 | 349.00 |
| 22 | C5 | 2025-04-01 00:00:00 | 999.00 |

Expected Output

`

+-----------+--------------+

|customer_id|purchase_count|

+-----------+--------------+

|         C1|             4|
|         C5|             3|

+-----------+--------------+

`

Explanation

Notes

What this PYSPARK challenge teaches you

“Amazon: Customers Active on Exactly 3 Days Last Month (PySpark)” is a easy-level PYSPARK challenge focused on Date Functions. Working through it gives you hands-on practice with countDistinct, to_date, groupBy, Date range, E-commerce — 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. F.to_date('purchase_ts') gives the calendar day; filter it to >= '2025-03-01' and < '2025-04-01'.
  2. groupBy('customer_id').agg(F.countDistinct(day), F.count('*')) gives days and purchases together.
  3. Keep shopping_days == 3 exactly, then select customer_id and purchase_count.
  4. orderBy('customer_id') before df_result.show().

Interview practice

This problem is good practice for data engineering interviews at companies such as Amazon. Interviewers use problems like 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 Date Functions.

Solve this challenge free on PySpark.in