Sessionize Pin Engagement Events with a 30-Minute Inactivity Gap

Last updated: November 29, 2026

Quick Overview

Given events(user_id, ts, event_type) with ts in epoch seconds, split each user's activity into sessions where a gap of more than 30 minutes starts a new session, and report each session's start, end, event count and number of saves.

Pinterest
Data Manipulation (SQL/Python)
Data Engineer
Pinterest
November 29, 2026
Data Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given events(user_id, ts, event_type) with ts in epoch seconds, split each user's activity into sessions where a gap of more than 30 minutes starts a new session, and report each session's start, end, event count and number of saves.

Sessions underpin Pinterest engagement metrics such as saves per session and session depth on the home feed. The interviewer checks the LAG plus running SUM pattern, precise gap semantics, and awareness of how session boundaries interact with day partitions in a warehouse.

What the Interviewer Expects
  • Uses LAG to compute the gap from the previous event per user
  • Marks the first event per user as a session start
  • Builds session ids with a running SUM of start flags
  • Aggregates per session with conditional counts
  • Discusses sessions crossing a daily partition boundary
Key Topics to Cover
sessionization
window functions
LAG
running sums
event data
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you compute this incrementally each hour without rescanning history?
  • How would you treat app backgrounding events that arrive with delayed timestamps?
  • What is the distribution you would check to choose the 30-minute threshold?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data
  • ts is epoch seconds; events may tie on ts (we order by ts, and ties fall in the same session).
  • Gap > 1800 seconds starts a new session; exactly 1800 continues the session.
  • event_type includes 'v...
Approach

Step 1: for each event, compare with the previous event of the same user; flag 1 when there is no previous event or the gap exceeds 30 minutes. Step 2: a running sum of those flags per user gives a se...


Submit Your Answer
Markdown supported