LeetCode #3673 — HARD

Find Zombie Sessions

Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.

Solve on LeetCode
The Problem

Problem Statement

Table: app_events

+------------------+----------+
| Column Name      | Type     | 
+------------------+----------+
| event_id         | int      |
| user_id          | int      |
| event_timestamp  | datetime |
| event_type       | varchar  |
| session_id       | varchar  |
| event_value      | int      |
+------------------+----------+
event_id is the unique identifier for this table.
event_type can be app_open, click, scroll, purchase, or app_close.
session_id groups events within the same user session.
event_value represents: for purchase - amount in dollars, for scroll - pixels scrolled, for others - NULL.

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:

  • The session duration is more than 30 minutes.
  • Has at least 5 scroll events.
  • The click-to-scroll ratio is less than 0.20 .
  • No purchases were made during the session.

Return the result table ordered by scroll_count in descending order, then by session_id in ascending order.

The result format is in the following example.

Example:

Input:

app_events table:

+----------+---------+---------------------+------------+------------+-------------+
| 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        |
+----------+---------+---------------------+------------+------------+-------------+

Output:

+------------+---------+--------------------------+--------------+
| session_id | user_id | session_duration_minutes | scroll_count |
+------------+---------+--------------------------+--------------+
| S001       | 201     | 35                       | 6            |
+------------+---------+--------------------------+--------------+

Explanation:

  • Session S001 (User 201):
    • Duration: 10:00:00 to 10:35:00 = 35 minutes (more than 30) 
    • Scroll events: 6 (at least 5) 
    • Click events: 0
    • Click-to-scroll ratio: 0/6 = 0.00 (less than 0.20) 
    • Purchases: 0 (no purchases) 
    • S001 is a zombie session (meets all criteria)
  • Session S002 (User 202):
    • Duration: 11:00:00 to 11:20:00 = 20 minutes (less than 30) 
    • Has a purchase event 
    • S002 is not a zombie session 
  • Session S003 (User 203):
    • Duration: 12:00:00 to 13:00:00 = 60 minutes (more than 30) 
    • Scroll events: 5 (at least 5) 
    • Click events: 1
    • Click-to-scroll ratio: 1/5 = 0.20 (not less than 0.20) 
    • Purchases: 0 (no purchases) 
    • S003 is not a zombie session (click-to-scroll ratio equals 0.20, needs to be less)
  • Session S004 (User 204):
    • Duration: 14:00:00 to 14:12:00 = 12 minutes (less than 30) 
    • Scroll events: 2 (less than 5) 
    • S004  is not a zombie session 

The result table is ordered by scroll_count in descending order, then by session_id in ascending order.

Roadmap

  1. Brute Force Baseline
  2. Core Insight
  3. Algorithm Walkthrough
  4. Edge Cases
  5. Full Annotated Code
  6. Interactive Study Demo
  7. Complexity Analysis
Step 01

Brute Force Baseline

Problem summary: Table: app_events +------------------+----------+ | Column Name | Type | +------------------+----------+ | event_id | int | | user_id | int | | event_timestamp | datetime | | event_type | varchar | | session_id | varchar | | event_value | int | +------------------+----------+ event_id is the unique identifier for this table. event_type can be app_open, click, scroll, purchase, or app_close. session_id groups events within the same user session. event_value represents: for purchase - amount in dollars, for scroll - pixels scrolled, for others - NULL. 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: The session duration is more than 30 minutes. Has at least 5 scroll events. The click-to-scroll ratio is less than 0.20 . No purchases were made

Baseline thinking

Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.

Pattern signal: General problem-solving

Example 1

{"headers":{"app_events":["event_id","user_id","event_timestamp","event_type","session_id","event_value"]},"rows":{"app_events":[[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]]}}
Step 02

Core Insight

What unlocks the optimal approach

  • No official hints in dataset. Start from constraints and look for a monotonic or reusable state.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03

Algorithm Walkthrough

Iteration Checklist

  1. Define state (indices, window, stack, map, DP cell, or recursion frame).
  2. Apply one transition step and update the invariant.
  3. Record answer candidate when condition is met.
  4. Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04

Edge Cases

Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Largest constraint values
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05

Full Annotated Code

Source-backed implementations are provided below for direct study and interview prep.

// Accepted solution for LeetCode #3673: Find Zombie Sessions
// Auto-generated Java example from py.
class Solution {
    public void exampleSolution() {
    }
}
// Reference (py):
// # Accepted solution for LeetCode #3673: Find Zombie Sessions
// import pandas as pd
// 
// 
// def find_zombie_sessions(app_events: pd.DataFrame) -> pd.DataFrame:
//     if not pd.api.types.is_datetime64_any_dtype(app_events["event_timestamp"]):
//         app_events["event_timestamp"] = pd.to_datetime(app_events["event_timestamp"])
// 
//     grouped = app_events.groupby(["session_id", "user_id"])
// 
//     result = grouped.agg(
//         session_duration_minutes=(
//             "event_timestamp",
//             lambda x: (x.max() - x.min()).total_seconds() // 60,
//         ),
//         scroll_count=("event_type", lambda x: (x == "scroll").sum()),
//         click_count=("event_type", lambda x: (x == "click").sum()),
//         purchase_count=("event_type", lambda x: (x == "purchase").sum()),
//     ).reset_index()
// 
//     result = result[
//         (result["session_duration_minutes"] >= 30)
//         & (result["click_count"] / result["scroll_count"] < 0.2)
//         & (result["purchase_count"] == 0)
//         & (result["scroll_count"] >= 5)
//     ]
// 
//     result = result.sort_values(
//         by=["scroll_count", "session_id"], ascending=[False, True]
//     ).reset_index(drop=True)
// 
//     return result[["session_id", "user_id", "session_duration_minutes", "scroll_count"]]
Step 06

Interactive Study Demo

Use this to step through a reusable interview workflow for this problem.

Press Step or Run All to begin.
Step 07

Complexity Analysis

Time
O(n)
Space
O(1)

Approach Breakdown

BRUTE FORCE
O(n²) time
O(1) space

Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.

OPTIMIZED
O(n) time
O(1) space

Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.

Shortcut: If you are using nested loops on an array, there is almost always an O(n) solution. Look for the right auxiliary state.
Coach Notes

Common Mistakes

Review these before coding to avoid predictable interview regressions.

Off-by-one on range boundaries

Wrong move: Loop endpoints miss first/last candidate.

Usually fails on: Fails on minimal arrays and exact-boundary answers.

Fix: Re-derive loops from inclusive/exclusive ranges before coding.