Sessionize Camera Heartbeats To Find When Devices Went Dark
Last updated: November 29, 2026
Quick Overview
Given a table of device heartbeats (device_id, ts) that may contain duplicates, write SQL that groups each device's heartbeats into online sessions, starting a new session whenever the gap since the previous heartbeat exceeds 10 minutes.
Axon
November 29, 20260
0
0 solved
Given a table of device heartbeats (device_id, ts) that may contain duplicates, write SQL that groups each device's heartbeats into online sessions, starting a new session whenever the gap since the previous heartbeat exceeds 10 minutes.
Axon's fleet of body cameras and docks reports telemetry, and device health dashboards need to show when a camera stopped checking in during a shift. This tests the gaps and islands pattern with window functions, deduplication, and reasoning about what the output means operationally.
What the Interviewer Expects
- Deduplicate repeated heartbeats first
- Use LAG partitioned by device to compute the gap
- Flag new sessions and take a running SUM to number them
- Aggregate start, end and count per session
- Explain how to derive offline gaps from consecutive sessions
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 list only offline gaps that overlap an officer's scheduled shift?
- How would you make this incremental for a table that grows by billions of rows a day?
- What if heartbeats can arrive late and out of order?
- How would the threshold change for devices on LTE versus Wi-Fi docks?
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
heartbeats(device_id, ts) with ts in UTC. Duplicates happen because devices retry. Threshold is strictly greater than 10 minutes to start a new session. Output: device, session number, start, end, h...
Approach
Classic gaps and islands. Deduplicate, then for each row compare ts to the previous ts for the same device with LAG. Mark is_new = 1 when there is no previous row or the gap exceeds 10 minutes...