Messenger Sessions That Survive Brief Trips to the Background
Last updated: November 29, 2026
Quick Overview
Messenger logs foreground, background and in-app action events. Define a session that continues if the user returns within five minutes of backgrounding the app, and ends otherwise; return each user's sessions as start and end times.
Meta
November 29, 20260
0
0 solved
Messenger logs foreground, background and in-app action events. Define a session that continues if the user returns within five minutes of backgrounding the app, and ends otherwise; return each user's sessions as start and end times.
A stateful Python sessionization problem in the style of Meta's onsite loops for engineers on Messenger and app performance teams. Unlike a simple gap rule, the session state depends on the app lifecycle, and the interviewer checks how carefully you model it and test the boundaries.
What the Interviewer Expects
- Models session state explicitly (no session, active, backgrounded)
- Ends a session only when background time exceeds the timeout
- Resumes the same session on a quick return
- Ends a session at the background time, not at the next foreground
- Closes open sessions at the end of each user's events
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 handle a missing background event when the app is killed?
- How would you compute this in a streaming job instead of batch?
- Would you count notification-triggered opens differently?
- How would you validate the definition against what users perceive as a session?
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
Events are (user_id, ts, kind) with kind foreground, background, or action. Rules I confirm: a session starts on a foreground or on an action when none is open (covers a missing foreground log...
Approach
Sort per user and walk events with three variables: start (None means no session), bg_since (None means foreground), and last_active. Before handling each event, check whether the background tim...