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

0

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
Gaps and islands
ROW_NUMBER
Window functions
Date arithmetic
Engagement metrics
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 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 Problems
Sample 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 d1<d2<…d_1 < d_2 < \dots per customer, consecutive days satisfy di−i=constantd_i - i = \text{constant}. So view_date - ROW_NUMBER() gives the same value (an anchor date) for every day in a streak....


Submit Your Answer
Markdown supported