PracHub
QuestionsLearningGuidesInterview Prep

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.

Last updated: Jul 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 9,000+ real questions from top companies.

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.

Related Coding 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)