Compute Leakage-Free Rolling Velocity Features for Card Fraud Scoring
Last updated: November 29, 2026
Quick Overview
For each card transaction, compute the number of transactions on the same card in the previous hour, the dollars spent in the previous 24 hours, the ratio of this amount to the card's prior average, and seconds since the previous transaction, using only data strictly before the current transaction.
Capital One
November 29, 20260
0
0 solved
For each card transaction, compute the number of transactions on the same card in the previous hour, the dollars spent in the previous 24 hours, the ratio of this amount to the card's prior average, and seconds since the previous transaction, using only data strictly before the current transaction.
Real-time fraud scoring at Capital One depends on velocity features, and the offline training pipeline must compute them exactly as the online system would at authorization time. The interviewer checks time-based rolling windows that exclude the current row, alignment of grouped rolling results, and ties.
What the Interviewer Expects
- Sorts by card and timestamp before rolling
- Uses time-based windows with closed='left' to exclude the current row
- Aligns groupby rolling output back to rows correctly
- Uses shift before expanding to avoid self-inclusion
- Discusses simultaneous transactions and train-serve consistency
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 compute distinct merchants in the last 24 hours?
- How would you make these features match an online feature store that updates with a few seconds of lag?
- How would you scale this to billions of rows with Spark?
- Should declined authorizations be counted in velocity?
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
txns(card_id, txn_ts, amount); timestamps to the second.- "Previous hour" means
(t - 1h, t), open at both ends relative to the current transaction; the current transaction is never in its own f...
Approach
Sort by (card_id, txn_ts) and reset the index. Group by card on a timestamp index and use rolling("1h", closed="left"). Because the frame is sorted by card first, the grouped rolling output is in ...