Sessionize Google Assistant Voice Queries on Inactivity and Device Switches
Last updated: November 29, 2026
Quick Overview
Given a DataFrame of Google Assistant queries with user, device and timestamp, assign session IDs where a session ends after 30 minutes of inactivity or when the user switches devices, then summarize each session.
0
0
0 solved
Given a DataFrame of Google Assistant queries with user, device and timestamp, assign session IDs where a session ends after 30 minutes of inactivity or when the user switches devices, then summarize each session.
A pandas sessionization variant in the style of Google Assistant analytics, where the same user talks to a phone, a Nest speaker and a car. The interviewer checks vectorized session boundaries with shift and cumsum, and whether you notice that shift crosses user boundaries unless guarded.
What the Interviewer Expects
- Sort by user and timestamp before computing differences
- Mark a boundary on a new user, a gap over 30 minutes, or a device change
- Build global session IDs with a cumulative sum of boundary flags
- Aggregate sessions to start, end, query count and duration
- Avoid row-by-row Python loops
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
- Should a switch from phone to speaker within 10 seconds really be a new session?
- How would you compute the share of sessions with a follow-up query?
- How would you do this in BigQuery?
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
df(user_id, device_id, ts, query_text), one row per query. A new session starts at the user's first query, after more than 30 minutes since their previous query, or when the device differs from the ...
Approach
After sorting by user and time, compare each row to the one above it. The time diff and device comparison are only meaningful when the previous row belongs to the same user, so the new-user flag is OR...