PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Data Manipulation (SQL/Python)/Apple

Detect sessions and gaps using SQL LEAD

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in SQL time-series and event-stream manipulation, focusing on window-function techniques for sessionization, timestamp arithmetic, and aggregation to produce session-level metrics, targeting Data Manipulation (SQL/Python) competencies relevant to data scientist roles.

  • Medium
  • Apple
  • Data Manipulation (SQL/Python)
  • Data Scientist

Detect sessions and gaps using SQL LEAD

Company: Apple

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: Medium

Interview Round: Technical Screen

Write a single ANSI-SQL query that (a) assigns per-user session_ids when the gap between consecutive events exceeds 30 minutes, (b) computes session_start, session_end, event_count, session_length_seconds, and next_session_gap_seconds (time from this session_end to the next session_start for the same user), and (c) uses window functions including LEAD at least once. Schema: - events(user_id INT, ts TIMESTAMP, action VARCHAR) Sample data: +---------+---------------------+---------+ | user_id | ts | action | +---------+---------------------+---------+ | 1 | 2025-08-01 09:00:00 | view | | 1 | 2025-08-01 09:10:00 | click | | 1 | 2025-08-01 10:00:00 | view | | 2 | 2025-08-01 12:00:00 | view | | 2 | 2025-08-01 12:20:00 | click | | 2 | 2025-08-01 12:45:00 | purchase| | 2 | 2025-08-01 14:00:00 | view | +---------+---------------------+---------+ Requirements: - Use LAG to detect new sessions and a running SUM to form session_ids per user. - Use LEAD(ts) to compute next_event_ts and derive next_session_gap_seconds. - Return columns: user_id, session_id, session_start, session_end, event_count, session_length_seconds, next_session_gap_seconds (NULL if no next session).

Quick Answer: This question evaluates proficiency in SQL time-series and event-stream manipulation, focusing on window-function techniques for sessionization, timestamp arithmetic, and aggregation to produce session-level metrics, targeting Data Manipulation (SQL/Python) competencies relevant to data scientist roles.

Related Interview Questions

  • Write queries to compute salary and budget stats - Apple (easy)
  • Analyze TSV File for User Page Visits and Patterns - Apple (Medium)
  • Compute and Rank Store Revenue by Region Using Pandas - Apple (Medium)
  • Explain Python lists, dicts, and concurrency - Apple (Medium)
Apple logo
Apple
Oct 13, 2025, 9:49 PM
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
6
0

Write a single ANSI-SQL query that (a) assigns per-user session_ids when the gap between consecutive events exceeds 30 minutes, (b) computes session_start, session_end, event_count, session_length_seconds, and next_session_gap_seconds (time from this session_end to the next session_start for the same user), and (c) uses window functions including LEAD at least once. Schema:

  • events(user_id INT, ts TIMESTAMP, action VARCHAR) Sample data: +---------+---------------------+---------+ | user_id | ts | action | +---------+---------------------+---------+ | 1 | 2025-08-01 09:00:00 | view | | 1 | 2025-08-01 09:10:00 | click | | 1 | 2025-08-01 10:00:00 | view | | 2 | 2025-08-01 12:00:00 | view | | 2 | 2025-08-01 12:20:00 | click | | 2 | 2025-08-01 12:45:00 | purchase| | 2 | 2025-08-01 14:00:00 | view | +---------+---------------------+---------+ Requirements:
  • Use LAG to detect new sessions and a running SUM to form session_ids per user.
  • Use LEAD(ts) to compute next_event_ts and derive next_session_gap_seconds.
  • Return columns: user_id, session_id, session_start, session_end, event_count, session_length_seconds, next_session_gap_seconds (NULL if no next session).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Data Manipulation (SQL/Python)•More Apple•More Data Scientist•Apple Data Scientist•Apple Data Manipulation (SQL/Python)•Data Scientist Data Manipulation (SQL/Python)
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.