Flag Card Charges Above Three Times the Trailing 30-Day Average
Last updated: November 29, 2026
Quick Overview
For each card transaction, compute the customer's average transaction amount over the previous 30 days (excluding the current one) and flag transactions more than three times that average, requiring at least five prior transactions.
Capital One
November 29, 20260
0
0 solved
For each card transaction, compute the customer's average transaction amount over the previous 30 days (excluding the current one) and flag transactions more than three times that average, requiring at least five prior transactions.
Capital One's fraud and card analytics teams lean on simple velocity and deviation rules as baselines before any model. The interviewer is checking time-based windows that exclude the current row, which is the point where most candidates leak the current transaction into its own baseline.
What the Interviewer Expects
- Defines the window as strictly before the current transaction and within 30 days
- Excludes the current row from its own average
- Applies a minimum history requirement so new customers are not over-flagged
- Chooses between a self-join and a RANGE window and explains the trade-off
- Discusses performance on a large transaction table
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 you rewrite this with a RANGE window frame in Postgres?
- Would a median or a percentile be a better baseline than the mean here?
- How would you compute the same feature incrementally in a streaming system?
- How would refunds affect the baseline?
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
transactions(txn_id, customer_id, txn_ts, amount)with purchases only (I would filter out refunds and payments first, since negative amounts drag the mean down).- The baseline is the 30 days stri...
Approach
Two valid shapes:
- A self-join on customer with
h.txn_ts < t.txn_ts AND h.txn_ts >= t.txn_ts - 30 days, then aggregate. Portable and easy to read; cost grows with transactions per customer per win...