Paytm: Find Double-Charged Payments and the Refund Amount (PySpark)

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

Problem

*A Paytm-style payments interview problem.*

Paytm's payment gateway sometimes charges a user more than once for the same payment: an app retry or a duplicate event is processed again, and every copy gets a new transaction_id. Finance needs the list of double charges so the extra copies can be refunded.

Using the PySpark DataFrame API (or Spark SQL) on transactions:

Assign the result to df_result with the columns user_id, merchant_id, amount, transaction_time, times_charged and refund_amount, sorted by user_id, then transaction_time, then merchant_id, then amount, and finish with df_result.show().

Schema

DataFrame: transactions

| Column | Type | Meaning |
| --- | --- | --- |
| transaction_id | string | Unique id of one event (every copy has its own) |
| user_id | string | The user who paid |
| merchant_id | string | The merchant who was paid |
| amount | double | Payment amount in rupees |
| transaction_time | string | When the payment was made, `yyyy-MM-dd HH:mm:ss` |
| status | string | `SUCCESS`, `FAILED` or `PENDING` |

Example Input: `transactions`

| transaction_id | user_id | merchant_id | amount | transaction_time | status |
| --- | --- | --- | --- | --- | --- |
| T101 | U1 | M10 | 500.0 | 2026-10-20 10:00:00 | SUCCESS |
| T102 | U1 | M10 | 500.0 | 2026-10-20 10:00:00 | SUCCESS |
| T103 | U2 | M20 | 250.0 | 2026-10-20 10:05:00 | SUCCESS |
| T104 | U1 | M10 | 700.0 | 2026-10-20 10:10:00 | SUCCESS |
| T105 | U2 | M20 | 250.0 | 2026-10-20 10:05:00 | SUCCESS |
| T106 | U2 | M20 | 250.0 | 2026-10-20 10:05:00 | FAILED |
| T107 | U3 | M30 | 100.0 | 2026-10-20 10:15:00 | SUCCESS |
| T108 | U3 | M30 | 100.0 | 2026-10-20 10:15:01 | SUCCESS |
| T109 | U4 | M40 | 1200.0 | 2026-10-20 11:00:00 | SUCCESS |
| T110 | U4 | M40 | 1200.0 | 2026-10-20 11:00:00 | SUCCESS |
| T111 | U4 | M40 | 1200.0 | 2026-10-20 11:00:00 | SUCCESS |
| T112 | U5 | M50 | 300.0 | 2026-10-20 12:00:00 | SUCCESS |
| T113 | U5 | M60 | 300.0 | 2026-10-20 12:00:00 | SUCCESS |
| T114 | U6 | M70 | 450.0 | 2026-10-20 12:30:00 | FAILED |
| T115 | U6 | M70 | 450.0 | 2026-10-20 12:30:00 | FAILED |
| T116 | U7 | M10 | 500.0 | 2026-10-20 10:00:00 | SUCCESS |

Expected Output

`

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

|user_id|merchant_id|amount|   transaction_time|times_charged|refund_amount|

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

|     U1|        M10| 500.0|2026-10-20 10:00:00|            2|        500.0|
|     U2|        M20| 250.0|2026-10-20 10:05:00|            2|        250.0|
|     U4|        M40|1200.0|2026-10-20 11:00:00|            3|       2400.0|

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

`

Explanation

Notes

What this PYSPARK challenge teaches you

“Paytm: Find Double-Charged Payments and the Refund Amount (PySpark)” is a easy-level PYSPARK challenge focused on GroupBy Aggregations. Working through it gives you hands-on practice with groupBy, count, filter, Duplicate detection, Payments, Fintech — 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. Filter status == 'SUCCESS' first: a FAILED or PENDING row charged nothing.
  2. groupBy('user_id', 'merchant_id', 'amount', 'transaction_time') and count the rows: that is times_charged. Leave transaction_id out, because every copy has a different one.
  3. Keep times_charged > 1, then refund_amount = amount * (times_charged - 1).
  4. orderBy('user_id', 'transaction_time', 'merchant_id', 'amount') before show(): the order is graded.

Where this comes up

Variations of this problem have been reported in interviews at Paytm. 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