1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-12 17:05:15 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/查找僵尸会话 [find-zombie-sessions].html
2025-09-25 00:20:19 +08:00

129 lines
5.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>表:<code>app_events</code></p>
<pre>
+------------------+----------+
| Column Name | Type |
+------------------+----------+
| event_id | int |
| user_id | int |
| event_timestamp | datetime |
| event_type | varchar |
| session_id | varchar |
| event_value | int |
+------------------+----------+
event_id 是这张表的唯一主键。
event_type 可以是 app_openclickscrollpurchase 或 app_close。
session_id 将事件按同一用户会话分组。
event_value 表示:对于 purchase - 美元金额,对于 scroll - 滚动的像素数,对于其它 - NULL。
</pre>
<p>编写一个解决方案来识别 <strong>僵尸会话</strong>,即用户看似活跃但表现出异常行为模式的会话。如果会话满足以下所有条件,则被视为 <strong>僵尸会话</strong></p>
<ul>
<li>会话时长&nbsp;<strong>超过</strong>&nbsp;<code>30</code>&nbsp;分钟。</li>
<li>至少有 <code>5</code> 次滚动事件。</li>
<li>点击滚动比率低于 <code>0.20</code></li>
<li>会话期间 <strong>没有进行任何购买</strong></li>
</ul>
<p>返回结果表按&nbsp;<code>scroll_count</code> <strong>降序</strong>&nbsp;排序,然后按&nbsp;<code>session_id</code> <strong>升序</strong>&nbsp;排序。</p>
<p>返回格式如下所示。</p>
<p>&nbsp;</p>
<p><strong class="example">示例:</strong></p>
<div class="example-block">
<p><strong>输入:</strong></p>
<p>app_events 表:</p>
<pre class="example-io">
+----------+---------+---------------------+------------+------------+-------------+
| event_id | user_id | event_timestamp | event_type | session_id | event_value |
+----------+---------+---------------------+------------+------------+-------------+
| 1 | 201 | 2024-03-01 10:00:00 | app_open | S001 | NULL |
| 2 | 201 | 2024-03-01 10:05:00 | scroll | S001 | 500 |
| 3 | 201 | 2024-03-01 10:10:00 | scroll | S001 | 750 |
| 4 | 201 | 2024-03-01 10:15:00 | scroll | S001 | 600 |
| 5 | 201 | 2024-03-01 10:20:00 | scroll | S001 | 800 |
| 6 | 201 | 2024-03-01 10:25:00 | scroll | S001 | 550 |
| 7 | 201 | 2024-03-01 10:30:00 | scroll | S001 | 900 |
| 8 | 201 | 2024-03-01 10:35:00 | app_close | S001 | NULL |
| 9 | 202 | 2024-03-01 11:00:00 | app_open | S002 | NULL |
| 10 | 202 | 2024-03-01 11:02:00 | click | S002 | NULL |
| 11 | 202 | 2024-03-01 11:05:00 | scroll | S002 | 400 |
| 12 | 202 | 2024-03-01 11:08:00 | click | S002 | NULL |
| 13 | 202 | 2024-03-01 11:10:00 | scroll | S002 | 350 |
| 14 | 202 | 2024-03-01 11:15:00 | purchase | S002 | 50 |
| 15 | 202 | 2024-03-01 11:20:00 | app_close | S002 | NULL |
| 16 | 203 | 2024-03-01 12:00:00 | app_open | S003 | NULL |
| 17 | 203 | 2024-03-01 12:10:00 | scroll | S003 | 1000 |
| 18 | 203 | 2024-03-01 12:20:00 | scroll | S003 | 1200 |
| 19 | 203 | 2024-03-01 12:25:00 | click | S003 | NULL |
| 20 | 203 | 2024-03-01 12:30:00 | scroll | S003 | 800 |
| 21 | 203 | 2024-03-01 12:40:00 | scroll | S003 | 900 |
| 22 | 203 | 2024-03-01 12:50:00 | scroll | S003 | 1100 |
| 23 | 203 | 2024-03-01 13:00:00 | app_close | S003 | NULL |
| 24 | 204 | 2024-03-01 14:00:00 | app_open | S004 | NULL |
| 25 | 204 | 2024-03-01 14:05:00 | scroll | S004 | 600 |
| 26 | 204 | 2024-03-01 14:08:00 | scroll | S004 | 700 |
| 27 | 204 | 2024-03-01 14:10:00 | click | S004 | NULL |
| 28 | 204 | 2024-03-01 14:12:00 | app_close | S004 | NULL |
+----------+---------+---------------------+------------+------------+-------------+
</pre>
<p><strong>输出:</strong></p>
<pre class="example-io">
+------------+---------+--------------------------+--------------+
| session_id | user_id | session_duration_minutes | scroll_count |
+------------+---------+--------------------------+--------------+
| S001 | 201 | 35 | 6 |
+------------+---------+--------------------------+--------------+
</pre>
<p><strong>解释:</strong></p>
<ul>
<li><strong>会话 S001 (User 201)</strong>:
<ul>
<li>时长10:00:00 到 10:35:00 = 35 分钟(大于 30 分钟)</li>
<li>滚动事件6至少 5 次)</li>
<li>点击事件0</li>
<li>点击滚动比率0/6 = 0.00(少于 0.20</li>
<li>购买数0没有购买</li>
<li>S001 是一个僵尸会话(满足所有条件)</li>
</ul>
</li>
<li><strong>会话 S002 (User 202)</strong>:
<ul>
<li>时长11:00:00 到 11:20:00 = 20 分钟(少于 30 分钟)</li>
<li>有一次购买事件</li>
<li>S002 不是一个僵尸会话</li>
</ul>
</li>
<li><strong>会话 S003 (User 203)</strong>:
<ul>
<li>时长12:00:00 到 13:00:00 = 60 分钟(超过 30 分钟)</li>
<li>滚动事件5至少 5 次)</li>
<li>点击事件1</li>
<li>点击滚动比率1/5 = 0.20(不少于 0.20</li>
<li>购买数0没有购买</li>
<li>S003 不是一个僵尸会话(点击滚动比率等于 0.20,需要更少)。</li>
</ul>
</li>
<li><strong>会话 S004 (User 204)</strong>:
<ul>
<li>时长14:00:00 到 14:12:00 = 12 分钟(少于 30 分钟)</li>
<li>滚动事件2少于&nbsp;5 次)</li>
<li>S004&nbsp; 不是一个僵尸会话</li>
</ul>
</li>
</ul>
<p>结果表按 scroll_count 降序排序,然后按 session_id 升序排序。</p>
</div>