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).
- The report runs on 15 April 2025, so last month is March 2025: every purchase from
2025-03-01 00:00:00up to and including2025-03-31 23:59:59. Purchases in February or April do not count at all. - A day is a calendar date. Several purchases on the same date are one day, whatever their times.
- Keep customers with exactly 3 different purchase days in March: not 2, not 4.
- For each of them return
purchase_count: how many purchases they made in March (every purchase, not every day).
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
- C1: two purchases on 2 March are one day; with 10 and 31 March that is 3 days from 4 purchases. The 31 March purchase is at 21:45, so a filter that stops at
'2025-03-31'(midnight) would lose it. - C2: 4 different days, so not exactly 3.
- C3: 28 February is outside last month. 1 March 00:00:00 is inside, but that gives only 2 March days.
- C4: 3 purchases, but on only 2 days (8 March twice).
- C5: 4, 18 and 25 March are 3 days; 1 April 00:00:00 is already this month, so
purchase_countis 3, not 4. - C7: 3 days in February and 1 in March. Only March matters: 1 day.
Notes
purchase_tsis a string with a time:"2025-03-31 21:45:00"is greater than"2025-03-31", so compare dates (F.to_date), not raw strings.- In production you would derive the month from
F.current_date(); here the report date is fixed (15 April 2025). purchases,spark,F(pyspark.sql.functions) andWindoware ready. Do not recreatepurchases.- Your code is also run on hidden datasets with other customers and dates, so it must compute the answer from
purchases. The row order is graded.
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
- countDistinct
- to_date
- groupBy
- Date range
- E-commerce
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:
- F.to_date('purchase_ts') gives the calendar day; filter it to >= '2025-03-01' and < '2025-04-01'.
- groupBy('customer_id').agg(F.countDistinct(day), F.count('*')) gives days and purchases together.
- Keep shopping_days == 3 exactly, then select customer_id and purchase_count.
- 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
- Monthly Revenue Report Using DATE_FORMAT
- Count Employees Hired in Each Year
- Find the Month With the Highest Number of New Hires
- Zomato: A User's Orders from the Last 30 Days
- Amazon: Customers Active on Exactly 3 Days Last Month
- IBM: Average VM Uptime Hours per Region
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.