Measure Gaps Between Payments to Spot Customers Drifting Off Autopay

Last updated: November 29, 2026

Quick Overview

Using a card payments table, compute the number of days between each customer's consecutive payments and flag customers whose most recent gap is more than double their own median gap and longer than 40 days.

Capital One
Data Manipulation (SQL/Python)
Data Scientist
Capital One
November 29, 2026
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Using a card payments table, compute the number of days between each customer's consecutive payments and flag customers whose most recent gap is more than double their own median gap and longer than 40 days.

Card servicing and retention teams at Capital One watch payment cadence as an early sign of a customer disengaging or heading toward delinquency. The interviewer is checking LAG usage, per-customer baselines, and whether you avoid comparing everyone to one global threshold.

What the Interviewer Expects
  • Uses LAG partitioned by customer and ordered by payment date
  • Computes each customer's own median gap rather than a global one
  • Isolates the most recent gap cleanly
  • Requires a minimum number of gaps before judging a customer
  • Explains why the latest gap should be measured to today if no payment has arrived
Key Topics to Cover
LAG
PERCENTILE_CONT
per-entity baselines
payment cadence
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 treat the open gap from the last payment to today?
  • How would you collapse multiple payments made in the same statement cycle?
  • How would you validate that this flag actually predicts delinquency?
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
  • payments(payment_id, customer_id, paid_date, amount, channel) where channel includes autopay, app, phone.
  • Several payments can land in one cycle (customers paying twice a month), so I coll...
Approach
  1. Deduplicate to distinct (customer_id, paid_date).
  2. Use LAG(paid_date) to compute the completed gaps.
  3. Compute each customer's median completed gap with PERCENTILE_CONT(0.5).
  4. Compute th...

Submit Your Answer
Markdown supported