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:
  • Signups - Contains user registration information
  • Confirmations - 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
Timeline (24 hours)User 110:00 AMUser 13:00 PM5 hours โœ“ (within 24h)User 212:00 PMUser 22:00 PM (+2 days)50 hours โœ— (exceeds 24h)Active UsersUser 1 โœ“Requests within 24hInactive UsersUser 2 โœ—Requests too far apart
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.
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
41.3K Views
Medium Frequency
~15 min Avg. Time
1.8K 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