Find the First Purchase That Pushed Each Card Over Its Limit
Last updated: November 29, 2026
Quick Overview
Given card transactions (purchases positive, payments and credits negative) and each account's credit limit, return for every account the first transaction at which the running balance exceeded the limit, along with that balance.
Capital One
November 29, 20260
0
0 solved
Given card transactions (purchases positive, payments and credits negative) and each account's credit limit, return for every account the first transaction at which the running balance exceeded the limit, along with that balance.
A typical warm-up SQL item in Capital One's data analyst and data scientist assessments, framed around card servicing data. The interviewer checks that you can write a running sum with a deterministic ordering and then pick the first qualifying row per group.
What the Interviewer Expects
- Uses a windowed SUM partitioned by account with an explicit frame
- Breaks timestamp ties deterministically with a transaction id
- Filters to over-limit rows before choosing the first per account
- Explains why payments must be negative for the running balance to be right
- Mentions the starting balance assumption
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 include a carried-over balance from the previous statement?
- How would you count how many distinct times an account crossed the limit, not just the first?
- What changes if pending authorizations also count toward available credit?
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
card_transactions(txn_id, account_id, txn_ts, amount): purchases are positive, payments, refunds and credits are negative.accounts(account_id, credit_limit).- Assume the balance starts at 0 ...
Approach
- Compute a running balance per account with
SUM(amount) OVER (PARTITION BY account_id ORDER BY txn_ts, txn_id). - Keep only rows where that balance exceeds the limit.
- Among those, take the ear...