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
Data Manipulation (SQL/Python)
Data Scientist
Capital One
November 29, 2026
Data Scientist
Online Assessment
Data Manipulation (SQL/Python)
Easy

0

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
window functions
running totals
ROW_NUMBER
tie-breaking
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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
  1. Compute a running balance per account with SUM(amount) OVER (PARTITION BY account_id ORDER BY txn_ts, txn_id).
  2. Keep only rows where that balance exceeds the limit.
  3. Among those, take the ear...

Submit Your Answer
Markdown supported