#3673

Find Zombie Sessions

Hard
Hash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

Utilize a single pass through the events to aggregate data for each session, allowing us to quickly evaluate the zombie criteria without redundant checks.

⚙️

Algorithm

3 steps
  1. 1Step 1: Group events by session_id and aggregate scroll counts, click counts, duration, and check for purchases.
  2. 2Step 2: Filter sessions based on the zombie criteria using the aggregated data.
  3. 3Step 3: Sort the results by scroll count in descending order.
solution.py7 lines
1def find_zombie_sessions(events):
2    from collections import defaultdict
3    sessions = defaultdict(lambda: {'scrolls': 0, 'clicks': 0, 'duration': 0, 'purchases': 0})
4    for event in events:
5        # Aggregate logic here
6    # Filter and sort logic here
7    return result

Complexity note: Single pass through events allows for linear time complexity, storing results in a hash map.

  • 1Sessions can be efficiently analyzed using aggregation techniques.
  • 2Understanding the criteria helps in designing the data structure.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.