အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ ကတော့ ကျွန်တော်တို့ challenge လေးရဲ့ နောက်ဆုံးနေ့ကိုရောက်ရှိလာပြီပဲဖြစ်ပါတယ်။ ဒီနေ့မှာတော့ ကွန်တော်ကတော့ difficulty hard level ရှိတဲ့ 3673.Find Zombies Sessions ဆိုတဲ့ ပုစ္ဆာလေးကို ရွေးချယ်ထားပါတယ်။ ဒီ ပုစ္ဆာလေးမှာဆိုရင် ကျွန်တော်တို့ကို app_events ဆိုတဲ့ table လေးကို အောက်ပါအတိုင်းပေးထားပါတယ်ဗျာ။
1Table: app_events2 3+------------------+----------+4| Column Name | Type | 5+------------------+----------+6| event_id | int |7| user_id | int |8| event_timestamp | datetime |9| event_type | varchar |10| session_id | varchar |11| event_value | int |12+------------------+----------+13event_id is the unique identifier for this table.14event_type can be app_open, click, scroll, purchase, or app_close.15session_id groups events within the same user session.16event_value represents: for purchase - amount in dollars, for scroll - pixels scrolled, for others - NULL.17Write 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:18 19The session duration is more than 30 minutes.20Has at least 5 scroll events.21The click-to-scroll ratio is less than 0.20 .22No purchases were made during the session.23Return the result table ordered by scroll_count in descending order, then by session_id in ascending order.24 25****ကျွန်တော်တို့ လုပ်ပေးဖို့လိုတာကတော့ user တွေရဲ့ Zombie Sessions တွေကို ရှာပေးရမှာပဲဖြစ်ပါတယ်။ Zombie Sessions ဖြစ်ဖို့ဆိုရင် criteria 4 ချက်နဲ့ ကိုက်ညီရမယ်လို့ပြောထားပါတယ်။
11. Session ကြာချိန်က မိနစ်သုံးဆယ်ကျော်ရမယ်။22. Scroll event က ငါးခု အနိမ့်ဆုံးဖြစ်ရမယ်။33. Click to scroll ratio က 0.20 (click count / scroll count )ထက်ငယ်ရမယ်။44. Session အတွင်း purchase မလုပ်ထားရဘူး။အထက်ပါ အချက်တွေနဲ့ ကိုက်ညီတဲ့ session တွေကို ရှာပြီး scroll_count DESC , session _id ASC နဲ့ order စီပေးရမှာပဲဖြစ်ပါတယ်။
1Example:2 3Input:4 5app_events table:6 7+----------+---------+---------------------+------------+------------+-------------+8| event_id | user_id | event_timestamp | event_type | session_id | event_value |9+----------+---------+---------------------+------------+------------+-------------+10| 1 | 201 | 2024-03-01 10:00:00 | app_open | S001 | NULL |11| 2 | 201 | 2024-03-01 10:05:00 | scroll | S001 | 500 |12| 3 | 201 | 2024-03-01 10:10:00 | scroll | S001 | 750 |13| 4 | 201 | 2024-03-01 10:15:00 | scroll | S001 | 600 |14| 5 | 201 | 2024-03-01 10:20:00 | scroll | S001 | 800 |15| 6 | 201 | 2024-03-01 10:25:00 | scroll | S001 | 550 |16| 7 | 201 | 2024-03-01 10:30:00 | scroll | S001 | 900 |17| 8 | 201 | 2024-03-01 10:35:00 | app_close | S001 | NULL |18| 9 | 202 | 2024-03-01 11:00:00 | app_open | S002 | NULL |19| 10 | 202 | 2024-03-01 11:02:00 | click | S002 | NULL |20| 11 | 202 | 2024-03-01 11:05:00 | scroll | S002 | 400 |21| 12 | 202 | 2024-03-01 11:08:00 | click | S002 | NULL |22| 13 | 202 | 2024-03-01 11:10:00 | scroll | S002 | 350 |23| 14 | 202 | 2024-03-01 11:15:00 | purchase | S002 | 50 |24| 15 | 202 | 2024-03-01 11:20:00 | app_close | S002 | NULL |25| 16 | 203 | 2024-03-01 12:00:00 | app_open | S003 | NULL |26| 17 | 203 | 2024-03-01 12:10:00 | scroll | S003 | 1000 |27| 18 | 203 | 2024-03-01 12:20:00 | scroll | S003 | 1200 |28| 19 | 203 | 2024-03-01 12:25:00 | click | S003 | NULL |29| 20 | 203 | 2024-03-01 12:30:00 | scroll | S003 | 800 |30| 21 | 203 | 2024-03-01 12:40:00 | scroll | S003 | 900 |31| 22 | 203 | 2024-03-01 12:50:00 | scroll | S003 | 1100 |32| 23 | 203 | 2024-03-01 13:00:00 | app_close | S003 | NULL |33| 24 | 204 | 2024-03-01 14:00:00 | app_open | S004 | NULL |34| 25 | 204 | 2024-03-01 14:05:00 | scroll | S004 | 600 |35| 26 | 204 | 2024-03-01 14:08:00 | scroll | S004 | 700 |36| 27 | 204 | 2024-03-01 14:10:00 | click | S004 | NULL |37| 28 | 204 | 2024-03-01 14:12:00 | app_close | S004 | NULL |38+----------+---------+---------------------+------------+------------+-------------+39Output:40 41+------------+---------+--------------------------+--------------+42| session_id | user_id | session_duration_minutes | scroll_count |43+------------+---------+--------------------------+--------------+44| S001 | 201 | 35 | 6 |45+------------+---------+--------------------------+--------------+46Explanation:47 48Session S001 (User 201):49Duration: 10:00:00 to 10:35:00 = 35 minutes (more than 30) 50Scroll events: 6 (at least 5) 51Click events: 052Click-to-scroll ratio: 0/6 = 0.00 (less than 0.20) 53Purchases: 0 (no purchases) 54S001 is a zombie session (meets all criteria)55Session S002 (User 202):56Duration: 11:00:00 to 11:20:00 = 20 minutes (less than 30) 57Has a purchase event 58S002 is not a zombie session 59Session S003 (User 203):60Duration: 12:00:00 to 13:00:00 = 60 minutes (more than 30) 61Scroll events: 5 (at least 5) 62Click events: 163Click-to-scroll ratio: 1/5 = 0.20 (not less than 0.20) 64Purchases: 0 (no purchases) 65S003 is not a zombie session (click-to-scroll ratio equals 0.20, needs to be less)66Session S004 (User 204):67Duration: 14:00:00 to 14:12:00 = 12 minutes (less than 30) 68Scroll events: 2 (less than 5) 69S004 is not a zombie session 70The result table is ordered by scroll_count in descending order, then by session_id in ascending order.71 72 ဒီ Example လေးမှာဆိုရင်တော့ Session id S001 ကသာ Zombie Session ဖြစ်နေပါတယ်။ Session Duration ကလည်း မိနစ်သုံးဆယ်ကျော်ကြာတယ်။ Scroll count ကလည်း ငါးခုအထက်ရှိတယ်။ Click to scroll ratio ကလည်း 0.20 ထက်ငယ်ပြီး Purchase လည်းမလုပ်ထားတဲ့ အတွက်လိုချင်တဲ့ အချက် အကုန်လုံးကိုက်ညီတာဖြစ်လို့ပဲဖြစ်ပါတယ်။
ကျွန်တော်ကတော့ အောက်ကအတိုင်းလေးပဲရေးဖြစ်ခဲ့ပါတယ်ဗျာ။
1**SQL**2 3 4WITH session_state AS (5 SELECT 6 session_id,7 user_id,8 TIMESTAMPDIFF(9 MINUTE, 10 MIN(event_timestamp),11 MAX(event_timestamp)) 12 AS session_duration_minutes,13 SUM(14 CASE 15 WHEN event_type = 'scroll' THEN 1 ELSE 016 END17 ) AS scroll_count,18 SUM(19 CASE 20 WHEN event_type = 'click' THEN 1 ELSE 021 END22 ) AS click_count,23 SUM(24 CASE 25 WHEN event_type = 'purchase' THEN 1 ELSE 026 END27 ) AS purchase_count28 FROM app_events GROUP BY session_id, user_id29)30SELECT 31 session_id,32 user_id,33 session_duration_minutes,34 scroll_count35FROM session_state 36WHERE 37 session_duration_minutes > 30 AND38 scroll_count >= 5 AND39 click_count / NULLIF(scroll_count, 0) < 0.20 AND40 purchase_count = 041ORDER BY scroll_count DESC, session_id ASC;ပထမဆုံးအနေနဲ့ ကျွန်တော် temporary result table လေးတစ်ခုဖြစ်တဲ့ session state ဆိုတဲ့ table လေးကို အရင် ယူလိုက်ပါတယ်။ ဒီ table လေးမှာဆိုရင် ကျွန်တော်တို့က user တစ်ယောက်ချင်းစီရဲ့ session ထဲမှာ result ရှာမှာဖြစ်လို့ session_id, user_id နှစ်လုလုံးနဲ့ Group By လုပ်ထားလိုက်ပါတယ်။ Session ကြာချိန်ကို ယူဖို့ကတော့ ကျွန်တော် TIMESTAMPDIFF Function လေးအသုံးပြုခဲ့ပါတယ်။
TIMESTAMPDIFF
"ဒီ function လေးမှာဆိုရင် parameter သုံးခုပါဝင်ပြီး ပထမတစ်ခုက ကျွန်တော်တို့ response လိုချင်တဲ့ unit ဖြစ်ပါတယ်။ Day or Hour or minute အစရှိသဖြင့်ပေါ့။ ဒုတိယမြောက်နဲ့ တတိယမြောက်ကတော့ ကျွန်တော်တို့ different လုပ်ပေးမဲ့ timestamp နှစ်ခုပဲဖြစ်ပါတယ်။"
ပြီးရင်တော့ သက်ဆိုင်ရာ scroll_count, click_count နဲ့ purchase_count တို့ကိုလည်းရှာထားလိုက်ပါတယ်။
ဒါဆိုရင်တော့ temporary result table လေးတော့ပြီးပြီ။ အောက်က ကျွန်တော်တို့ရဲ့ Select လုပ်ထားတာကိုပြန်ကြည့်မယ်ဆိုရင် စောနက session state ဆိုတဲ့ table လေးထဲကပဲ အကုန်ဆွဲထုတ်ထားပြီး Where နောက်မှာတော့ criteria လေးချက်ကိုစစ်ထားလိုက်ပါတယ်။
11. Session ကြာချိန်က မိနစ်သုံးဆယ်ကျော်ရမယ်။22. Scroll event က ငါးခု အနိမ့်ဆုံးဖြစ်ရမယ်။33. Click to scroll ratio က 0.20 (click count / scroll count )ထက်ငယ်ရမယ်။44. Session အတွင်း purchase မလုပ်ထားရဘူး။ဒီနေရာမှာသုံးခု မြောက် click to scroll ratio မှာတော့ ကျွန်တော် NULLIF function လေးကို အသုံးပြုထားပါတယ်။
ဒီ function ရဲ့ အလုပ်လုပ်ပုံကတော့ ရှေ့က parameter နဲ့ နောက်က parameter value တူနေရင် null value return ပြန်ပေးတာပဲဖြစ်ပါတယ်။ ဘာကြောင့် ကျွန်တော်ဒီနေရာမှာအသုံးပြုလည်းဆိုရင် scroll count က 0 ဖြစ်နေရင် division By 0 ဆိုတဲ့ error တက်လာနိုင်လို့ပဲဖြစ်ပါတယ်။ 0 မဟုတ်ဘဲ null ဆိုရင်တော့ null ပဲရရှိမှာဖြစ်လိုပါ။
ဒါဆိုရင် ကျွန်တော် တို့ query လေးက လိုချင်တဲ့ လေးချက်နဲ့ ကိုက်ညီသွားပြီပဲဖြစ်ပါတယ်။ နောက်ဆုံးအနေနဲ့ order ကို လိုချင်တဲ့ အတိုင်း scroll_count DESC နဲ့ session_id ASC စီပေးလိုက်ရင် အချက်အားလုံးနဲ့ ကိုက်ညီသွားမှာပဲဖြစ်ပါတယ်။ ကျွန်တော်တို့ရဲ့ နောက်ဆုံးပုစ္ဆာလေးကလည်းပြီးဆုံးသွားခဲ့ပြီပဲဖြစ်ပါတယ်။ အားလုံးပဲကျေးဇူးအများကြီးတင်ပါတယ်ဗျာ။
30 Days Of LeetCode Database Final Day - 3673. Find Zombie Sessions
Watch on YouTubeDiscussion
Join the conversation
How do you feel about this article?
Comments
Sign in to join the conversation
Sign in to be the first to comment!
Share Your Article
Share with your professional network
Recent Articles

AWS - Application Load Balancer
Elastic Load Balancing (ELB) ELB ဆိုတာကတော့ request တွေကို တစ်နေရာတည်းမှ လက်ခံကာ Amazon EC2 instances၊ containers, etc.....

Terraform Day 3: Benefits of Terraform State
Terraform ကိုလေ့လာ တဲ့အခါ ကျွန််တော်တို့ရဲ့ Project Folder ထဲမှာ terraform.tfstate ဆိုတဲ့ ဖိုင်လေးကို တွေ့ဖူးကြပါလိမ့်မယ...

Terraform Day 2: Essential IaC Principles You Must Know
မနေ့ကတော့ Terraform အကြောင်း အကြမ်းဖျင်း Concept ကို ပြောပြခဲ့ပြီးပြီဆိုတော့ ဒီနေ့မှာတော့ Terraform ကို Professional ကျက...

TCP/IP Protocol
အားလုံးပဲမင်္ဂလာပါ။ ဒီနေ့ ကျွန်တော်တို့ TCP/IP Protocol အကြောင်း ဆွေးနွေးသွားပါမယ်။ ပထမဆုံးအနေနဲ့ TCP/IP ရဲ့ History လေး...

Terraform Day 1: Introduction to IAC and Terraform
ကျွန်တော်တို့ cloud အကြောင်း စပြောကြပြီဆိုရင် အရင်ဆုံး ခေါင်းထဲရောက်လာတာ Console ထဲဝင်၊ UI ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


