User Activities within Time Bounds - Problem
User Activities within Time Bounds

You're analyzing user session data for a streaming platform to identify highly engaged users. The platform supports two types of sessions: Viewer (watching content) and Streamer (creating content).

Your task is to find users who demonstrate consistent engagement by having at least two sessions of the same type within a 12-hour window. This helps identify dedicated viewers or content creators who are likely to be loyal platform users.

Key Requirements:
• Sessions must be of the same type (both Viewer or both Streamer)
• Maximum gap between sessions: 12 hours
• Return user IDs sorted in ascending order

Table: Sessions
Contains user session data with start/end times and session types

Input & Output

example_1.sql — Basic Case
$ Input: Sessions: [(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')]
Output: [102]
💡 Note: User 101 has different session types (Viewer vs Streamer), so doesn't qualify. User 102 has two Viewer sessions 2 hours apart, which is within the 12-hour window.
example_2.sql — Multiple Qualifying Users
$ Input: Sessions: [(102,'2023-11-02 12:00:00','2023-11-02 13:00:00',6,'Streamer'), (102,'2023-11-02 16:00:00','2023-11-02 17:00:00',8,'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: [102, 103]
💡 Note: User 102 qualifies from previous Viewer sessions. User 103 has two Viewer sessions 10 hours apart (20:00 to 09:00 next day = 13 hours, but 23:00 to 09:00 = 10 hours from session end to start).
example_3.sql — Edge Case: Exactly 12 Hours
$ Input: Sessions: [(104,'2023-11-01 08:00:00','2023-11-01 09:00:00',12,'Streamer'), (104,'2023-11-01 20:00:00','2023-11-01 21:00:00',13,'Streamer')]
Output: [104]
💡 Note: User 104 has two Streamer sessions exactly 12 hours apart (08:00 to 20:00), which meets the ≤12 hour requirement.

Visualization

Tap to expand
User Session Timeline AnalysisUser 102 (Qualifies ✓)13:00 V15:00 V2h gap ✓User 103 (Qualifies ✓)20:00 V09:00 V13h gap ≤12h? Check session endUser 101 (Does Not Qualify ✗)08:00 V10:00 SDifferent types ✗LegendViewer SessionViewer Session (User 103)Streamer SessionValid time gap (≤12h)Invalid (different types)
Understanding the Visualization
1
Group by User & Type
Organize sessions by user and session type (Viewer/Streamer)
2
Apply Time Window
Check if any two sessions of same type are within 12 hours
3
Identify Qualifying Users
Users with at least one qualifying pair are included in results
Key Takeaway
🎯 Key Insight: Use database indexes on (user_id, session_type, session_start) for optimal performance when checking time-based proximity conditions.

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

With proper indexes, join operation is O(n log n). Without indexes, could degrade to O(n²)

n
2n
Linearithmic
Space Complexity
O(n)

Space for result set and join operation intermediate results

n
2n
Linearithmic Space

Constraints

  • 1 ≤ number of sessions ≤ 104
  • 1 ≤ user_id ≤ 106
  • session_start < session_end for all sessions
  • session_type ∈ {'Viewer', 'Streamer'}
  • All session_id values are unique
Asked in
Netflix 45 Amazon 38 Meta 32 Google 28
23.5K Views
Medium-High Frequency
~25 min Avg. Time
847 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