|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3673\. Find Zombie Sessions |
| 5 | + |
| 6 | +Hard |
| 7 | + |
| 8 | +Table: `app_events` |
| 9 | + |
| 10 | + +------------------+----------+ |
| 11 | + | Column Name | Type | |
| 12 | + +------------------+----------+ |
| 13 | + | event_id | int | |
| 14 | + | user_id | int | |
| 15 | + | event_timestamp | datetime | |
| 16 | + | event_type | varchar | |
| 17 | + | session_id | varchar | |
| 18 | + | event_value | int | |
| 19 | + +------------------+----------+ |
| 20 | + event_id is the unique identifier for this table. |
| 21 | + event_type can be app_open, click, scroll, purchase, or app_close. |
| 22 | + session_id groups events within the same user session. |
| 23 | + event_value represents: for purchase - amount in dollars, for scroll - pixels scrolled, for others - NULL. |
| 24 | + |
| 25 | +Write a solution to identify **zombie sessions, **sessions where users appear active but show abnormal behavior patterns. A session is considered a **zombie session** if it meets ALL the following criteria: |
| 26 | + |
| 27 | +* The session duration is **more than** `30` minutes. |
| 28 | +* Has **at least** `5` scroll events. |
| 29 | +* The **click-to-scroll ratio** is less than `0.20` . |
| 30 | +* **No purchases** were made during the session. |
| 31 | + |
| 32 | +Return _the result table ordered by_ `scroll_count` _in **descending** order, then by_ `session_id` _in **ascending** order_. |
| 33 | + |
| 34 | +The result format is in the following example. |
| 35 | + |
| 36 | +**Example:** |
| 37 | + |
| 38 | +**Input:** |
| 39 | + |
| 40 | +app\_events table: |
| 41 | + |
| 42 | + +----------+---------+---------------------+------------+------------+-------------+ |
| 43 | + | event_id | user_id | event_timestamp | event_type | session_id | event_value | |
| 44 | + +----------+---------+---------------------+------------+------------+-------------+ |
| 45 | + | 1 | 201 | 2024-03-01 10:00:00 | app_open | S001 | NULL | |
| 46 | + | 2 | 201 | 2024-03-01 10:05:00 | scroll | S001 | 500 | |
| 47 | + | 3 | 201 | 2024-03-01 10:10:00 | scroll | S001 | 750 | |
| 48 | + | 4 | 201 | 2024-03-01 10:15:00 | scroll | S001 | 600 | |
| 49 | + | 5 | 201 | 2024-03-01 10:20:00 | scroll | S001 | 800 | |
| 50 | + | 6 | 201 | 2024-03-01 10:25:00 | scroll | S001 | 550 | |
| 51 | + | 7 | 201 | 2024-03-01 10:30:00 | scroll | S001 | 900 | |
| 52 | + | 8 | 201 | 2024-03-01 10:35:00 | app_close | S001 | NULL | |
| 53 | + | 9 | 202 | 2024-03-01 11:00:00 | app_open | S002 | NULL | |
| 54 | + | 10 | 202 | 2024-03-01 11:02:00 | click | S002 | NULL | |
| 55 | + | 11 | 202 | 2024-03-01 11:05:00 | scroll | S002 | 400 | |
| 56 | + | 12 | 202 | 2024-03-01 11:08:00 | click | S002 | NULL | |
| 57 | + | 13 | 202 | 2024-03-01 11:10:00 | scroll | S002 | 350 | |
| 58 | + | 14 | 202 | 2024-03-01 11:15:00 | purchase | S002 | 50 | |
| 59 | + | 15 | 202 | 2024-03-01 11:20:00 | app_close | S002 | NULL | |
| 60 | + | 16 | 203 | 2024-03-01 12:00:00 | app_open | S003 | NULL | |
| 61 | + | 17 | 203 | 2024-03-01 12:10:00 | scroll | S003 | 1000 | |
| 62 | + | 18 | 203 | 2024-03-01 12:20:00 | scroll | S003 | 1200 | |
| 63 | + | 19 | 203 | 2024-03-01 12:25:00 | click | S003 | NULL | |
| 64 | + | 20 | 203 | 2024-03-01 12:30:00 | scroll | S003 | 800 | |
| 65 | + | 21 | 203 | 2024-03-01 12:40:00 | scroll | S003 | 900 | |
| 66 | + | 22 | 203 | 2024-03-01 12:50:00 | scroll | S003 | 1100 | |
| 67 | + | 23 | 203 | 2024-03-01 13:00:00 | app_close | S003 | NULL | |
| 68 | + | 24 | 204 | 2024-03-01 14:00:00 | app_open | S004 | NULL | |
| 69 | + | 25 | 204 | 2024-03-01 14:05:00 | scroll | S004 | 600 | |
| 70 | + | 26 | 204 | 2024-03-01 14:08:00 | scroll | S004 | 700 | |
| 71 | + | 27 | 204 | 2024-03-01 14:10:00 | click | S004 | NULL | |
| 72 | + | 28 | 204 | 2024-03-01 14:12:00 | app_close | S004 | NULL | |
| 73 | + +----------+---------+---------------------+------------+------------+-------------+ |
| 74 | + |
| 75 | +**Output:** |
| 76 | + |
| 77 | + +------------+---------+--------------------------+--------------+ |
| 78 | + | session_id | user_id | session_duration_minutes | scroll_count | |
| 79 | + +------------+---------+--------------------------+--------------+ |
| 80 | + | S001 | 201 | 35 | 6 | |
| 81 | + +------------+---------+--------------------------+--------------+ |
| 82 | + |
| 83 | +**Explanation:** |
| 84 | + |
| 85 | +* **Session S001 (User 201)**: |
| 86 | + * Duration: 10:00:00 to 10:35:00 = 35 minutes (more than 30) |
| 87 | + * Scroll events: 6 (at least 5) |
| 88 | + * Click events: 0 |
| 89 | + * Click-to-scroll ratio: 0/6 = 0.00 (less than 0.20) |
| 90 | + * Purchases: 0 (no purchases) |
| 91 | + * S001 is a zombie session (meets all criteria) |
| 92 | +* **Session S002 (User 202)**: |
| 93 | + * Duration: 11:00:00 to 11:20:00 = 20 minutes (less than 30) |
| 94 | + * Has a purchase event |
| 95 | + * S002 is not a zombie session |
| 96 | +* **Session S003 (User 203)**: |
| 97 | + * Duration: 12:00:00 to 13:00:00 = 60 minutes (more than 30) |
| 98 | + * Scroll events: 5 (at least 5) |
| 99 | + * Click events: 1 |
| 100 | + * Click-to-scroll ratio: 1/5 = 0.20 (not less than 0.20) |
| 101 | + * Purchases: 0 (no purchases) |
| 102 | + * S003 is not a zombie session (click-to-scroll ratio equals 0.20, needs to be less) |
| 103 | +* **Session S004 (User 204)**: |
| 104 | + * Duration: 14:00:00 to 14:12:00 = 12 minutes (less than 30) |
| 105 | + * Scroll events: 2 (less than 5) |
| 106 | + * S004 is not a zombie session |
| 107 | + |
| 108 | +The result table is ordered by scroll\_count in descending order, then by session\_id in ascending order. |
| 109 | + |
| 110 | +## Solution |
| 111 | + |
| 112 | +```sql |
| 113 | +# Write your MySQL query statement below |
| 114 | +SELECT |
| 115 | + session_id, |
| 116 | + user_id, |
| 117 | + TIMESTAMPDIFF(MINUTE, MIN(event_timestamp), MAX(event_timestamp)) AS session_duration_minutes, |
| 118 | + SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END) AS scroll_count -- NOSONAR |
| 119 | +FROM |
| 120 | + app_events |
| 121 | +GROUP BY |
| 122 | + session_id, |
| 123 | + user_id |
| 124 | +HAVING |
| 125 | + TIMESTAMPDIFF(MINUTE, MIN(event_timestamp), MAX(event_timestamp)) > 30 |
| 126 | + AND SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END) > 4 -- NOSONAR |
| 127 | + AND ( |
| 128 | + CAST(SUM(CASE WHEN event_type = 'click' THEN 1 ELSE 0 END) AS DOUBLE) / |
| 129 | + NULLIF(SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END), 0) -- NOSONAR |
| 130 | + ) < 0.2 |
| 131 | + AND SUM(CASE WHEN event_type = 'purchase' THEN 1 ELSE 0 END) = 0 |
| 132 | +ORDER BY |
| 133 | + scroll_count DESC, |
| 134 | + session_id ASC; |
| 135 | +``` |
0 commit comments