Compute Each Customer's Longest Streak of Consecutive Viewing Days
Last updated: November 29, 2026
Quick Overview
Given a table of video viewing events with timestamps, return each customer's longest run of consecutive calendar days with at least one view, along with the start and end dates of that streak.
Amazon
November 29, 20260
0
0 solved
Given a table of video viewing events with timestamps, return each customer's longest run of consecutive calendar days with at least one view, along with the start and end dates of that streak.
Engagement streaks are used to understand habit formation for Prime Video and other subscription products. This is a gaps-and-islands problem, a favorite of Amazon SQL screens, testing window functions and careful deduplication of days.
What the Interviewer Expects
- Reduce events to distinct customer-days
- Use date minus ROW_NUMBER to label islands
- Aggregate islands to get streak length and bounds
- Pick the longest with a deterministic tie-break
- Handle time zones for day boundaries
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 return the current streak instead of the longest?
- How would you allow one missed day without breaking the streak?
- How would you handle customers in different time zones?
- How would you compute the distribution of streak lengths?
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
Table viewing_events(customer_id, view_ts TIMESTAMP, title_id). Many events per day per customer. A day counts if at least one view occurred. Use the customer's local date if a time zone column exis...
Approach
For distinct days per customer, consecutive days satisfy . So view_date - ROW_NUMBER() gives the same value (an anchor date) for every day in a streak....