Rolling Seven-Day Chargeback Rate to Flag Risky Merchants
Last updated: November 29, 2026
Quick Overview
Given transactions(txn_id, merchant_id, created_at) and chargebacks(cb_id, txn_id), compute each merchant's trailing 7-day chargeback rate per calendar day, counting days with zero activity, and return merchant-days with at least 100 transactions and a rate above 1%.
PayPal
November 29, 20260
0
0 solved
Given transactions(txn_id, merchant_id, created_at) and chargebacks(cb_id, txn_id), compute each merchant's trailing 7-day chargeback rate per calendar day, counting days with zero activity, and return merchant-days with at least 100 transactions and a rate above 1%.
Card networks put merchants into monitoring programs around a 1% chargeback ratio, and PayPal's merchant risk team watches the same metric to adjust reserves and holds. The interviewer checks that a rolling window is defined over calendar days, not rows, and that the attribution date for chargebacks is a deliberate choice.
What the Interviewer Expects
- Build a date spine so ROWS BETWEEN 6 PRECEDING means seven calendar days
- Attribute chargebacks to the original transaction date (or justify the alternative)
- LEFT JOIN daily counts onto the spine and COALESCE zeros
- Apply a minimum volume threshold to avoid noisy small merchants
- Avoid integer division
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
- How would results change if you attributed chargebacks to the date they were filed?
- Chargebacks arrive up to 120 days late; how do you report a rate that is still maturing?
- How would you scale this to millions of merchants daily?
- Would you rank merchants by rate, by count, or by a Bayesian shrunk rate?
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
Each chargeback references a transaction. Two valid definitions exist: by transaction date (what share of the week's sales came back disputed, a cohort view) or by dispute date (the networks' monitori...
Approach
Aggregate daily transaction and chargeback counts per merchant, generate a continuous spine of dates for each merchant between first and last activity, left join counts onto the spine, then take a 7-r...