Longest Streak of Consecutive On-Time Monthly Card Payments
Last updated: November 29, 2026
Quick Overview
Given one row per account per statement month with a flag for whether that month's payment was on time, return each account's longest run of consecutive on-time months with its start and end month; missing months break a streak.
Capital One
November 29, 20260
0
0 solved
Given one row per account per statement month with a flag for whether that month's payment was on time, return each account's longest run of consecutive on-time months with its start and end month; missing months break a streak.
This is a gaps-and-islands problem dressed in Capital One's credit-building context, where on-time payment streaks feed credit line increase eligibility. The interviewer is checking whether you can turn consecutive-period logic into a grouping key without loops.
What the Interviewer Expects
- Converts months to an integer index so consecutive months differ by one
- Uses index minus ROW_NUMBER to build an island key
- Treats both late months and missing months as breaks
- Picks the longest island with a deterministic tie-break
- Returns accounts with zero on-time months instead of dropping them
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 the current active streak instead of the longest?
- How would you allow one grace month without breaking the streak?
- How would this change if statements are on a 30-day cycle rather than calendar months?
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
monthly_payments(account_id, stmt_month, paid_on_time)wherestmt_monthis the first day of the statement month.- A late month ends the streak. A month with no row (no statement because of zero...
Approach
Filter to on-time months. Map each month to year * 12 + month. Within an account, consecutive on-time months have indexes that rise by one while ROW_NUMBER also rises by one, so `month_idx - ROW_N...