Users That Actively Request Confirmation Messages - Problem
Find Users with Frequent Confirmation Requests
You're building a security system that monitors user behavior for suspicious activity. Your task is to identify users who are making frequent confirmation requests within a short time window.
Given two tables:
Your goal is to find all user IDs that requested confirmation messages twice within a 24-hour window. The outcome of the confirmation (confirmed/timeout) doesn't matter - only the timing of requests.
Key Points:
You're building a security system that monitors user behavior for suspicious activity. Your task is to identify users who are making frequent confirmation requests within a short time window.
Given two tables:
Signups- Contains user registration informationConfirmations- Contains confirmation request logs with timestamps and outcomes
Your goal is to find all user IDs that requested confirmation messages twice within a 24-hour window. The outcome of the confirmation (confirmed/timeout) doesn't matter - only the timing of requests.
Key Points:
- Two messages exactly 24 hours apart are considered within the window
- Only request timestamps matter, not the action results
- A user can have multiple valid pairs of requests
Input & Output
Basic Example - Multiple Users
$
Input:
Signups:
| user_id | time_stamp |
|---------|--------------------|
| 3 | 2020-03-21 10:16:13 |
| 7 | 2020-01-04 13:57:59 |
| 2 | 2020-07-29 23:09:44 |
| 6 | 2020-12-09 10:39:37 |
Confirmations:
| user_id | time_stamp | action |
|---------|--------------------|-----------|
| 3 | 2021-01-06 03:30:46 | timeout |
| 3 | 2021-01-06 20:30:46 | confirmed |
| 7 | 2021-06-12 11:57:29 | confirmed |
| 7 | 2021-06-13 11:57:30 | timeout |
โบ
Output:
| user_id |
|---------|
| 3 |
| 7 |
๐ก Note:
User 3 requested confirmations at 03:30:46 and 20:30:46 on the same day (17 hours apart, within 24-hour window). User 7 requested confirmations on June 12th and 13th at almost the same time (exactly 24 hours and 1 second apart, which is within the 24-hour window since we include exactly 24 hours).
Edge Case - Exactly 24 Hours Apart
$
Input:
Signups:
| user_id | time_stamp |
|---------|--------------------|
| 1 | 2020-01-01 00:00:00 |
| 2 | 2020-01-01 00:00:00 |
Confirmations:
| user_id | time_stamp | action |
|---------|--------------------|-----------|
| 1 | 2021-01-01 12:00:00 | confirmed |
| 1 | 2021-01-02 12:00:00 | timeout |
| 2 | 2021-01-01 12:00:00 | confirmed |
| 2 | 2021-01-02 12:00:01 | timeout |
โบ
Output:
| user_id |
|---------|
| 1 |
๐ก Note:
User 1 has requests exactly 24 hours apart (included). User 2 has requests 24 hours and 1 second apart (excluded, since it exceeds the 24-hour window).
No Active Users
$
Input:
Signups:
| user_id | time_stamp |
|---------|--------------------|
| 1 | 2020-01-01 00:00:00 |
| 2 | 2020-01-01 00:00:00 |
Confirmations:
| user_id | time_stamp | action |
|---------|--------------------|-----------|
| 1 | 2021-01-01 12:00:00 | confirmed |
| 1 | 2021-01-03 12:00:00 | timeout |
| 2 | 2021-01-01 12:00:00 | confirmed |
โบ
Output:
| user_id |
|---------|
(empty result)
๐ก Note:
User 1 has requests 48 hours apart (exceeds 24-hour window). User 2 has only one confirmation request. No users qualify as actively requesting confirmation messages.
Constraints
- 1 โค Number of signups โค 1000
- 1 โค Number of confirmations โค 104
- user_id is a positive integer
- time_stamp is a valid datetime
- action is either 'confirmed' or 'timeout'
- Each user can have multiple confirmation requests
- Two messages exactly 24 hours apart are considered within the window
Visualization
Tap to expand
Understanding the Visualization
1
Collect All Requests
Gather all confirmation requests with timestamps
2
Group by User
Organize requests by user_id to analyze individual patterns
3
Sort Chronologically
Order each user's requests by timestamp
4
Apply Sliding Window
Check if any two consecutive requests fall within 24-hour window
5
Identify Active Users
Flag users with requests meeting the time criteria
Key Takeaway
๐ฏ Key Insight: Use SQL window functions to efficiently compare consecutive timestamps within each user's request history, eliminating expensive cross-joins while maintaining accuracy.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code