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:
- A payment is identified by
user_id,merchant_id,amountandtransaction_timetogether. Two rows are copies of the same payment only when all four match.transaction_idis never part of the identity. - Only rows with
status = 'SUCCESS'are charges.FAILEDandPENDINGrows charged nothing and are ignored. times_chargedis the number of successful copies of a payment.- The first charge is legitimate; every extra copy is refunded:
refund_amount = amount * (times_charged - 1). - Return only payments charged more than once, one row per payment.
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
- U1 paid M10 ₹500.0 at 10:00:00 twice (T101, T102): charged 2 times, ₹500.0 refunded. U1's ₹700.0 payment at 10:10:00 is a different payment, charged once.
- U2 has three copies, but T106
FAILED: charged 2 times, ₹250.0 refunded. - U4 was charged 3 times: the two extra copies are refunded, ₹2400.0.
- U3 paid again one second later, U5 paid two different merchants and U7 is a different user from U1: none of these are copies.
- U6's copies all
FAILED, so U6 was never charged and does not appear.
Notes
transactions,spark,F(pyspark.sql.functions) andWindoware ready. Do not recreatetransactions.- Your code is also run on hidden datasets with other users and payments, so it must compute the answer from
transactions. - The row order is graded.
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
- groupBy
- count
- filter
- Duplicate detection
- Payments
- Fintech
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:
- Filter status == 'SUCCESS' first: a FAILED or PENDING row charged nothing.
- 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.
- Keep times_charged > 1, then refund_amount = amount * (times_charged - 1).
- 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
- Calculate Total Sales and Average Order Value by Customer
- Analyze Sales Data to Find Top Performers by Region
- Count Rows per City
- Analyze Product Sales by Region (PySpark)
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
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.