Calculate Longest Transaction Streak for Each User
Company: Capital One
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates proficiency in data manipulation and time-series sequence analysis, specifically measuring the longest run of consecutive successful transactions per user from transactional logs.
Tables
transactions(transaction_id INTEGER, user_id INTEGER, amount DECIMAL(10,2), transaction_date DATE, merchant VARCHAR(100), status VARCHAR(20))
Hints
- Pre-aggregate to DISTINCT (user_id, transaction_date) for successful rows only, so multiple transactions on the same day don't inflate the streak length.
- In PostgreSQL, subtracting one DATE from another (a - b) gives an integer day count — use it with LAG to detect whether consecutive days are exactly 1 apart.