Month-End Balance Snapshots From Sparse Savings Balance Change Events
Last updated: November 29, 2026
Quick Overview
Savings accounts emit an event only when the balance changes. Produce one row per account per month from its first event through the current month with the month-end balance, carrying the last known balance forward through months without events.
Capital One
November 29, 20260
0
0 solved
Savings accounts emit an event only when the balance changes. Produce one row per account per month from its first event through the current month with the month-end balance, carrying the last known balance forward through months without events.
Capital One 360 savings reporting, interest accrual checks and deposit forecasting need dense month-end snapshots, but the source system only records changes. The interviewer is checking month spine generation per account, picking the last event per month, and forward filling without IGNORE NULLS.
What the Interviewer Expects
- Generates a per-account month spine from first event to the current month
- Picks the last event in each month with a tie-breaker
- Forward fills using a count-based grouping trick or LAST_VALUE IGNORE NULLS
- Explains why a simple LAG does not fill multi-month gaps
- Considers closed accounts and when the spine should stop
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 stop the spine when an account is closed?
- How would you produce average daily balance instead of month-end balance?
- How would you do the same with pandas resample and ffill?
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
balance_events(event_id, account_id, event_ts, balance_after).- Month-end balance =
balance_afterof the last event in the month, or the previous month's value if no event happened. - Spine: fr...
Approach
- Spine per account via
generate_seriesin a LATERAL join. - Last event per account and month with
ROW_NUMBER() ... ORDER BY event_ts DESC, event_id DESC. - Left join spine to last events, leav...