Reels Viewing Sessions with an Inactivity Gap and a Two-Hour Cap
Last updated: November 29, 2026
Quick Overview
Split each user's Reels view events into sessions that end after 30 minutes of inactivity or once a session would run longer than two hours from its start, and return session start, end and event count.
Meta
November 29, 20260
0
0 solved
Split each user's Reels view events into sessions that end after 30 minutes of inactivity or once a session would run longer than two hours from its start, and return session start, end and event count.
A sessionization variant in the style of Meta's onsite data engineering interviews, where continuous Reels scrolling can produce unrealistically long sessions under a pure gap rule. The interviewer checks that you see why the cap makes the logic sequential and how you would still run it at scale.
What the Interviewer Expects
- Applies both the gap rule and the length cap in one pass
- Resets the session start correctly when the cap triggers
- Explains why the cap cannot be done with LAG and a running sum alone
- Handles single-event sessions and empty input
- Describes running it per user in Spark with sorted groups
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
- Why do teams cap session length at all, and what does it do to time-spent metrics?
- How would you express the cap in SQL if you had to?
- Should the cap split at exactly the two-hour mark rather than at the next event?
- How would you test the job against a reference implementation?
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
Input per user is a list of view timestamps in seconds, possibly unsorted. Rules: start a new session if the gap since the previous event is over 1,800 seconds, or if the event is over 7,200 seconds a...
Approach
The gap rule alone is order-independent across sessions, so SQL can do it with LAG and a cumulative sum. The cap depends on where the current session started, which depends on earlier cap decisions,...