Daily Active Minutes When Instagram Sessions Cross Midnight
Last updated: November 29, 2026
Quick Overview
Build sessions from Instagram foreground events with a 30-minute inactivity gap, then compute each user's active minutes per calendar day, splitting any session that crosses midnight between the two days.
Meta
November 29, 20260
0
0 solved
Build sessions from Instagram foreground events with a 30-minute inactivity gap, then compute each user's active minutes per calendar day, splitting any session that crosses midnight between the two days.
A harder SQL problem in Meta's data engineering onsite, where time-spent tables feed daily dashboards. The interviewer checks sessionization with window functions and, more importantly, that a session from 23:50 to 00:20 contributes 10 minutes to each day rather than 30 to one.
What the Interviewer Expects
- Builds sessions with LAG and a running sum of new-session flags
- Derives session start and end from the grouped events
- Splits sessions across calendar days with a date spine join
- Uses GREATEST and LEAST to clip each piece to its day
- Explains time zone choice for the day boundary
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
- Which time zone should define the day for a global product?
- How would you make this incremental so yesterday's partition is not rebuilt every day?
- How would you treat the trailing time after the last event in a session?
- What if a session spans more than two days because of a logging bug?
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
ig_events(user_id, event_ts) holds foreground events (taps, scrolls, views) in the user's local time. A new session starts when the gap from the previous event exceeds 30 minutes. Session length is ...
Approach
Step 1: LAG per user, flag a new session when the gap exceeds 30 minutes or there is no previous event, then a running SUM of flags gives a session number. Step 2: min and max timestamp per sessio...