KatInterview

Flagging Heavy VR Headset Users: Streak Detection in SQL

SQL & DataModel answer
MediumModel answerSQL & Datasqlgaps-and-islandswindow-functions

Last updated

Define a precise rule for excessive headset use, then write SQL that counts flagged users, active users, and the flagged share over a trailing 30-day window.

Scenario

A wellbeing team at a VR hardware company wants to measure how common marathon headset sessions are. Usage is logged in a table named vr_sessions, with one row per session:

user_idsession_startsession_enddeviceevent_date
12024-05-01 10:00:002024-05-01 12:30:00Quest22024-05-01
12024-05-02 09:00:002024-05-02 13:45:00Quest22024-05-02
22024-05-01 18:00:002024-05-01 18:45:00Quest32024-05-01
32024-05-01 20:10:002024-05-01 22:10:00Quest22024-05-01
32024-05-02 20:05:002024-05-02 22:30:00Quest22024-05-02

event_date is the calendar date on which the session began.

Task

  1. Define the rule. Propose a precise, SQL-expressible definition of an "unhealthy" user. One reasonable example: a user who, within the last 30 days, had a single session longer than 120 minutes on each of at least 3 consecutive calendar days. State every assumption you make (what "continuous" means, strict vs. non-strict thresholds, how midnight-crossing sessions are dated, which days belong to the window).
  2. Write the query. Using your rule, return one row with:
    • (a) the number of unhealthy users,
    • (b) the total number of active users (users with at least one session in the window),
    • (c) the percentage of active users who are unhealthy.
  3. Check it against the sample rows above and explain the result.
  4. Discuss edge cases and how the rule or query would change for them.

Hints

Hint 1

Start by turning each session into a duration in minutes (end minus start), then collapse to one row per user per calendar day, keeping only the days that satisfy your 'heavy' condition.

Hint 2

For consecutive days, subtract a per-user ROW_NUMBER (ordered by date) from the date. Dates in an unbroken run share the same difference, so that value works as a group key; a group with 3 or more rows is a qualifying streak.

Hint 3

Make sure each user-date appears only once before applying the row-number trick, use the same 30-day filter for numerator and denominator, force decimal division, and protect against a zero denominator.