Average Watch Session Length per Device from Heartbeat Pings
Last updated: November 29, 2026
Quick Overview
Facebook Watch emits a heartbeat ping every 30 seconds while a video plays. Given (user_id, device, ts) pings, split them into sessions whenever two pings are more than two minutes apart and return the average session length per device.
Meta
November 29, 20260
0
0 solved
Facebook Watch emits a heartbeat ping every 30 seconds while a video plays. Given (user_id, device, ts) pings, split them into sessions whenever two pings are more than two minutes apart and return the average session length per device.
A Python data-wrangling question in Meta data engineering screens, where heartbeat-style logging is common for video and Live. The interviewer wants to see sessionization logic written cleanly, a defined duration rule, and awareness of out-of-order logs.
What the Interviewer Expects
- Groups pings per user and device and sorts by time
- Splits sessions when the gap exceeds the threshold
- Defines duration as last minus first plus one ping interval
- Closes the final open session for each user
- Runs in O(n log n) and explains the cost
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 do the same thing in SQL with LAG and a running sum?
- What if a user watches on two devices at once?
- How would you handle duplicate pings from client retries?
- How would you report the median instead of the mean?
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 is a list of (user_id, device, ts) with ts in seconds, possibly unsorted, possibly with duplicate pings. A session ends when the next ping is more than 120 seconds later. Since a single ping...
Approach
Group timestamps by (user, device), sort each list, then walk it once keeping start and prev. When the gap from prev exceeds 120, close the session, add its length to that device's totals, and s...