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
Data Manipulation (SQL/Python)
Data Scientist
Capital One
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Hard

0

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
gaps and islands
ROW_NUMBER
consecutive periods
credit line increase
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 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 Problems
Sample Answer
Clarify the Data
  • monthly_payments(account_id, stmt_month, paid_on_time) where stmt_month is 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...


Submit Your Answer
Markdown supported