Sessionize CVS App Events With a Thirty Minute Inactivity Gap

Last updated: November 29, 2026

Quick Overview

Given app_events (device_id, event_ts, screen), assign a session_id to every event where a new session starts after more than 30 minutes of inactivity, then report sessions per device per day and median session length.

CVS Health
Data Manipulation (SQL/Python)
Data Engineer
CVS Health
November 29, 2026
Data Engineer
Onsite
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given app_events (device_id, event_ts, screen), assign a session_id to every event where a new session starts after more than 30 minutes of inactivity, then report sessions per device per day and median session length.

The CVS Pharmacy app mixes quick tasks like showing a pickup barcode with longer ones like browsing ExtraCare deals, so digital teams sessionize raw clickstream before computing engagement. This checks the LAG plus running SUM pattern and thinking about edge cases.

What the Interviewer Expects
  • Uses LAG to compute time since previous event per device
  • Marks session starts with a gap threshold
  • Uses a cumulative SUM to build session ids
  • Aggregates session duration and counts correctly
  • Handles single-event sessions and midnight crossings
Key Topics to Cover
sessionization
LAG
running sum
gaps and islands
clickstream
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 make this incremental for a daily batch job when sessions cross midnight?
  • Should a backgrounded app with a push notification open start a new session?
  • How would you compute median in an engine without PERCENTILE_CONT?
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

app_events(device_id, event_ts TIMESTAMP, screen). A session ends when the next event from the same device arrives more than 30 minutes later. Session date is the date of the session start. Duration...

Approach
  1. LAG the timestamp per device.
  2. is_new = 1 when there is no previous event or the gap exceeds 30 minutes.
  3. Running SUM of is_new per device gives a session number; combine with device_id for...

Submit Your Answer
Markdown supported

Related Questions