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
Contains user session data with start/end times and session types
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: SessionsContains 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
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²)
⚡ Linearithmic
Space Complexity
O(n)
Space for result set and join operation intermediate results
⚡ 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code