Build a 90-Day Customer Feature Table From Raw Card Transactions
Last updated: November 29, 2026
Quick Overview
Write a pandas function that, given raw card transactions, a list of customers and an as-of date, returns one row per customer with 90-day spend, ticket size, merchant diversity, foreign and cash advance shares, and days since last transaction.
Capital One
November 29, 20260
0
0 solved
Write a pandas function that, given raw card transactions, a list of customers and an as-of date, returns one row per customer with 90-day spend, ticket size, merchant diversity, foreign and cash advance shares, and days since last transaction.
Capital One data science take-homes often hand you transaction-level data and ask for model-ready features. The reviewer checks point-in-time windows anchored on an as-of date, named aggregations, and explicit handling of customers who had no activity in the window.
What the Interviewer Expects
- Filters strictly to the window before the as-of date
- Uses groupby with named aggregations for readability
- Reindexes to the full customer list so inactive customers are present
- Chooses sensible fills: zero for counts, a sentinel for recency, NaN for ratios
- Explains why the as-of date prevents leakage into labels
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 these features for many as-of dates efficiently?
- Which of these features would you expect to drift during the holiday season?
- How would you add a feature for the trend in spend between the last 30 and the prior 60 days?
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(customer_id, txn_ts, amount, merchant_id, country, mcc); purchases only.- Window:
[as_of - 90 days, as_of). Anything on or afteras_ofis future relative to the label and must be exclude...
Approach
Filter to the window, add boolean helper columns, aggregate with named aggregations, derive recency from the last transaction, then reindex to the customer list. Fill rules: counts and totals become 0...