Users That Actively Request Confirmation Messages Problem
Description
LeetCode Problem 1939.
Table: Signups
+----------------+----------+
| Column Name | Type |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
+----------------+----------+
user_id is the primary key for this table.
Each row contains information about the signup time for the user with ID user_id.
Table: Confirmations
+----------------+----------+
| Column Name | Type |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
| action | ENUM |
+----------------+----------+
(user_id, time_stamp) is the primary key for this table.
user_id is a foreign key with a reference to the Signups table.
action is an ENUM of the type ('confirmed', 'timeout')
Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed ('confirmed') or expired without confirming ('timeout').
Write an SQL query to find the IDs of the users that requested a confirmation message twice within a 24-hour window. Two messages exactly 24 hours apart are considered to be within the window. The action does not affect the answer, only the request time.
Return the result table in any order.
The query result format is in the following example:
Signups table:
+---------+---------------------+
| 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 table:
+---------+---------------------+-----------+
| user_id | time_stamp | action |
+---------+---------------------+-----------+
| 3 | 2021-01-06 03:30:46 | timeout |
| 3 | 2021-01-06 03:37:45 | timeout |
| 7 | 2021-06-12 11:57:29 | confirmed |
| 7 | 2021-06-13 11:57:30 | confirmed |
| 2 | 2021-01-22 00:00:00 | confirmed |
| 2 | 2021-01-23 00:00:00 | timeout |
| 6 | 2021-10-23 14:14:14 | confirmed |
| 6 | 2021-10-24 14:14:13 | timeout |
+---------+---------------------+-----------+
Result table
+---------+
| user_id |
+---------+
| 2 |
| 3 |
| 6 |
+---------+
User 2 requested two messages within exactly 24 hours of each other, so we include them.
User 3 requested two messages within 6 minutes and 59 seconds of each other, so we include them.
User 6 requested two messages within 23 hours, 59 minutes, and 59 seconds of each other, so we include them.
User 7 requested two messages within 24 hours and 1 second of each other, so we exclude them from the answer.
Solution
Approach: LEAD to the Next Request
If a user has any two requests within 24 hours of each other, then some pair of consecutive requests (in time order) is also within 24 hours, because the requests between them only make the gaps smaller. So it is enough to compare each request with the user's next one.
LEAD(time_stamp) OVER (PARTITION BY user_id ORDER BY time_stamp)puts each user's next request time on the row.- Keep the rows where
next_time <= time_stamp + INTERVAL 24 HOUR. The comparison is<=because two requests exactly 24 hours apart count (user 2), while 24 hours and 1 second do not (user 7). A user's last request hasnext_time = NULL, the comparison is not true, and the row is dropped. SELECT DISTINCT user_id, since a user can have several close pairs.
Only the request times matter, so neither action nor the Signups table is used.
The Pandas version sorts by user and time, takes the gap to the previous request with groupby(...).diff(), and keeps the users with a gap of at most pd.Timedelta(hours=24).
WITH next_requests AS (
SELECT user_id,
time_stamp,
LEAD(time_stamp) OVER (PARTITION BY user_id ORDER BY time_stamp) AS next_time
FROM Confirmations
)
SELECT DISTINCT user_id
FROM next_requests
-- Exactly 24 hours apart still counts.
WHERE next_time <= time_stamp + INTERVAL 24 HOUR;