← Apple Interview Insights

Apple·Data Scientist·Technical Phone Screen·Senior

Senior
Jul 2026

Summary

Apple data scientist technical screen, one question, all SQL. They handed me a sessionization problem with a strict set of requirements and just watched me work through it.

Questions Asked (1)

Q1

Write a single ANSI-SQL query that assigns per-user session IDs when the gap between consecutive events exceeds 30 minutes, then computes session_start, session_end, event_count, session_length_seconds, and next_session_gap_seconds using window functions including LEAD.

Data ModelingAlgorithms & Data StructuresProduct Analytics & Metrics
Author's notes

This is the kind of question that looks manageable until you're actually writing it live.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a CTE to compute the time gap between consecutive events per user with LAG, flag new sessions when the gap exceeds 30 minutes or is null, then assign session IDs via a running SUM of the flag. Finally, aggregate per session to compute session_start, session_end, event_count, session_length_seconds, and use LEAD to get the next session's start for next_session_gap_seconds.

Pro tip: Explicitly handle the first event per user (LAG returns NULL) and tie-breaking for identical timestamps; mention that the 30-minute threshold is strict (> 30 minutes) and that session_length_seconds should be computed as the difference between session_end and session_start.

1. Compute time gaps between consecutive events

Use LAG(event_time) OVER (PARTITION BY user_id ORDER BY event_time) to get the previous event time, then calculate the gap in seconds (or minutes) between the current and previous event.

2. Flag new sessions

Create a flag that is 1 when the gap is NULL (first event) or greater than 30 minutes (1800 seconds), and 0 otherwise. This identifies session boundaries.

3. Assign session IDs

Use SUM(flag) OVER (PARTITION BY user_id ORDER BY event_time) to generate a running session number, which becomes the session ID (e.g., user_id || '-' || session_num).

4. Aggregate session metrics

Group by user_id and session_id to compute session_start = MIN(event_time), session_end = MAX(event_time), event_count = COUNT(*), and session_length_seconds = DATEDIFF(second, MIN(event_time), MAX(event_time)).

5. Compute next session gap

Use LEAD(session_start) OVER (PARTITION BY user_id ORDER BY session_start) to get the next session's start time, then compute next_session_gap_seconds as the difference between that and the current session_end.

Key Points to Mention

  • Use of LAG to compute time differences between consecutive events per user.
  • Session boundary condition: gap > 30 minutes (1800 seconds) or first event (NULL gap).
  • Running SUM of session flags to assign session IDs.
  • Aggregation with MIN/MAX/COUNT for session_start, session_end, event_count, and session_length_seconds.
  • Use of LEAD on session_start to compute next_session_gap_seconds.
  • Handling of NULLs and ensuring correct ordering with ties (e.g., using event_time and a tie-breaker like event_id).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.