User Activities within Time Bounds - Problem

Given a Sessions table with user session information, find all users who have had at least two sessions of the same type (either 'Viewer' or 'Streamer') with a maximum gap of 12 hours between sessions.

The table contains:

  • user_id: User identifier
  • session_start and session_end: Session timing
  • session_id: Unique session identifier
  • session_type: Either 'Viewer' or 'Streamer'

Return the result ordered by user_id in ascending order.

Table Schema

Sessions
Column Name Type Description
user_id int User identifier
session_start datetime Session start timestamp
session_end datetime Session end timestamp
session_id PK int Unique session identifier
session_type enum Session type: 'Viewer' or 'Streamer'
Primary Key: session_id
Note: Each session has a unique session_id and belongs to a user with a specific type

Input & Output

Example 1 — Multiple Users with Different Session Patterns
Input Table:
user_id session_start session_end session_id session_type
101 2023-11-01 08:00:00 2023-11-01 09:00:00 1 Viewer
101 2023-11-01 10:00:00 2023-11-01 11:00:00 2 Streamer
102 2023-11-01 13:00:00 2023-11-01 14:00:00 3 Viewer
102 2023-11-01 15:00:00 2023-11-01 16:00:00 4 Viewer
101 2023-11-02 09:00:00 2023-11-02 10:00:00 5 Viewer
102 2023-11-02 12:00:00 2023-11-02 13:00:00 6 Streamer
101 2023-11-02 13:00:00 2023-11-02 14:00:00 7 Streamer
102 2023-11-02 16:00:00 2023-11-02 17:00:00 8 Viewer
103 2023-11-01 08:00:00 2023-11-01 09:00:00 9 Viewer
103 2023-11-02 20:00:00 2023-11-02 23:00:00 10 Viewer
103 2023-11-03 09:00:00 2023-11-03 10:00:00 11 Viewer
Output:
user_id
102
💡 Note:

User 101: Has sessions of different types (Viewer and Streamer) but no two sessions of the same type within 12 hours.

User 102: Has Viewer sessions (IDs 3 and 4) with only 2 hours gap, qualifying the user.

User 103: Has multiple Viewer sessions, including sessions 10 and 11 with 13 hours gap, but since they have qualifying pairs elsewhere, user 103 is included.

Example 2 — Edge Case with Exact 12-Hour Gap
Input Table:
user_id session_start session_end session_id session_type
201 2023-11-01 08:00:00 2023-11-01 09:00:00 1 Viewer
201 2023-11-01 20:00:00 2023-11-01 21:00:00 2 Viewer
202 2023-11-01 10:00:00 2023-11-01 11:00:00 3 Streamer
202 2023-11-01 22:01:00 2023-11-01 23:00:00 4 Streamer
Output:
user_id
201
💡 Note:

User 201: Has exactly 12 hours gap between Viewer sessions (8:00 to 20:00), which meets the ≤12 hours criteria.

User 202: Has 12 hours and 1 minute gap between Streamer sessions, which exceeds the 12-hour limit.

Constraints

  • 1 ≤ user_id ≤ 10000
  • session_id is unique for each session
  • session_type is either 'Viewer' or 'Streamer'
  • session_start < session_end for all sessions
  • Time gap is measured between session start times

Visualization

Tap to expand
User Activities within Time Bounds INPUT: Sessions Table user_id type timestamp 1 login 10:00 1 login 18:00 1 view 11:00 2 login 09:00 2 login 22:00 3 click 14:00 Constraint: Max gap = 12 hours Timeline Visualization 0h 12h 24h OK gap ALGORITHM STEPS 1 Group by user_id, type Partition sessions 2 Sort by timestamp Order within groups 3 Calculate time gaps LAG() window function 4 Filter gap <= 12 hrs Return distinct users WITH ranked AS ( SELECT user_id, type, timestamp, LAG(timestamp) OVER( PARTITION BY user_id, type ORDER BY time ) as prev_time ) SELECT WHERE gap<=12h ORDER BY user_id FINAL RESULT Users with repeated sessions within 12-hour window User 1 - login sessions 10:00 --> 18:00 Gap: 8 hours [OK] User 2 - login sessions 09:00 --> 22:00 Gap: 13 hours [FAIL] User 3 - only 1 session No pair [FAIL] Output: [user_id: 1] Key Insight: Use window functions (LAG/LEAD) to compare consecutive timestamps within each user-type group. PARTITION BY (user_id, session_type) creates independent groups, ORDER BY timestamp ensures correct sequence. TutorialsPoint - User Activities within Time Bounds | Optimal Solution (Window Functions)
Asked in
Meta 28 Google 24 Amazon 19 Netflix 15
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen