Deduplicate: Keep the Latest Record per Customer (PySpark)

PYSPARK coding challenge · Difficulty: medium · Topic: Window Functions · +75 XP

Problem

Using the PySpark DataFrame API, return exactly one row per customer_id — the

row with the latest updated_timestamp — ordered by customer_id.

Assign it to result and call result.show().

Example Input - `transactions`

| customer_id | transaction_id | amount | updated_timestamp |
| --- | --- | --- | --- |
| 101 | T001 | 500 | 2025-05-10 10:00:00 |
| 101 | T001 | 500 | 2025-05-11 12:30:00 |
| 102 | T002 | 700 | 2025-05-11 09:00:00 |
| 102 | T002 | 700 | 2025-05-11 09:15:00 |
| 103 | T003 | 900 | 2025-05-12 08:00:00 |

Expected Output

| customer_id | transaction_id | amount | updated_timestamp |
| --- | --- | --- | --- |
| 101 | T001 | 500 | 2025-05-11 12:30:00 |
| 102 | T002 | 700 | 2025-05-11 09:15:00 |
| 103 | T003 | 900 | 2025-05-12 08:00:00 |

Notes

What this PYSPARK challenge teaches you

“Deduplicate: Keep the Latest Record per Customer (PySpark)” is a medium-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with window, row_number, deduplication, partitionBy, delta — 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. Rank rows within each customer_id using a window ordered by updated_timestamp DESC.
  2. row_number() over the window gives 1 to the newest row — keep rn == 1.
  3. Drop the helper column and orderBy('customer_id') so the output is stable.

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 medium and covers Window Functions.

Solve this challenge free on PySpark.in