Sessionize Retail Clickstream Events With Window Functions
Last updated: November 29, 2026
Quick Overview
Given `events(user_id, event_time, page, event_type)` from an e-commerce client, assign a session ID to every event where a new session starts after 30 minutes of inactivity, then report per-day session count, median session length and the share of sessions that reached checkout.
EPAM Systems
November 29, 20260
0
0 solved
Given `events(user_id, event_time, page, event_type)` from an e-commerce client, assign a session ID to every event where a new session starts after 30 minutes of inactivity, then report per-day session count, median session length and the share of sessions that reached checkout.
EPAM data engineering projects for retail and media clients often rebuild analytics that the client previously bought from a vendor, and sessionization is a classic building block. The interviewer checks the gaps-and-islands pattern with `LAG` and a running sum, and careful aggregation afterwards.
What the Interviewer Expects
- Use LAG over user-partitioned, time-ordered rows to find gaps
- Turn new-session flags into session numbers with a running SUM
- Aggregate to one row per session before computing daily metrics
- Use a conditional aggregate for the checkout flag
- State how ties in timestamps and midnight-crossing sessions are handled
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 implement the same logic in Spark Structured Streaming with late data?
- How would you make the job incremental so it does not reprocess all history every night?
- Which session belongs to which day if it crosses midnight?
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
One row per event, timestamps in UTC, possible duplicate timestamps per user. Session boundary is a gap strictly greater than 30 minutes. A session is attributed to the date of its first event. Checko...
Approach
Gaps and islands: compute the previous event time per user with LAG; flag a row as a new session when there is no previous event or the gap exceeds 30 minutes; a running SUM of flags per user give...