Flag Probable Duplicate Payments Within Ten Minutes Using Window Functions
Last updated: November 29, 2026
Quick Overview
From a payments table, return every successful payment that follows an earlier successful payment from the same debit account to the same beneficiary for the same amount within 10 minutes, along with the id of that earlier payment.
TCS
November 29, 20260
0
0 solved
From a payments table, return every successful payment that follows an earlier successful payment from the same debit account to the same beneficiary for the same amount within 10 minutes, along with the id of that earlier payment.
Duplicate debits from retried UPI or NEFT requests are a real operational issue on the payments platforms TCS builds and supports for banks. The interviewer checks partitioned LAG, correct filtering order (status before windowing), and interval arithmetic.
What the Interviewer Expects
- Filter to successful payments before computing the window
- Partition by debit account, beneficiary and amount
- Order deterministically with a tie-breaker
- Use LAG to compare against the immediately previous payment
- Return both ids so operations can reverse one
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- Why is comparing only to the immediately previous payment enough here?
- How would you allow a small amount tolerance instead of exact equality?
- How would you prevent duplicates at write time rather than detect them afterwards?
- How do you run this incrementally every five minutes?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
payments(payment_id, debit_account, beneficiary_account, amount, created_at, status). Only status = 'SUCCESS' rows can be duplicates (a failed attempt followed by a success is a normal retry). Dup...
Approach
Filter to successes, then within each (debit_account, beneficiary_account, amount) partition ordered by time, LAG gives the previous payment. Keep rows where the gap is at most 10 minutes. Comparing...