Compute max planes shot and session count
Problem: Count User Sessions from Events
You are provided with a list of events and a timeout duration. Each event is represented as a pair [userId, time], where time is an integer. The events in the input list may be in an arbitrary order.
For each individual user, their events must first be sorted chronologically by time. A session is defined by the temporal proximity of consecutive events for the same user. Specifically, two consecutive events for a user belong to the same session if the time difference between them is less than or equal to the specified timeout value. If the time difference exceeds timeout, the later event initiates a new session.
Your task is to calculate and return the total number of distinct sessions across all users.
Input
events: A list of[userId, time]pairs (e.g.,[[1, 10], [2, 5], [1, 12], ...]).timeout: A non-negative integer representing the maximum allowed time difference between consecutive events within the same session.
Output
- An integer representing the total count of sessions across all users.
AI interview prep
Core dilemma
Efficiently grouping, sorting, and aggregating time-series event data from an unsorted, multi-user input stream to derive session counts, considering potential scale.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute max planes shot and session count
Problem: Count User Sessions from Events
You are provided with a list of events and a timeout duration. Each event is represented as a pair [userId, time], where time is an integer. The events in the input list may be in an arbitrary order.
For each individual user, their events must first be sorted chronologically by time. A session is defined by the temporal proximity of consecutive events for the same user. Specifically, two consecutive events for a user belong to the same session if the time difference between them is less than or equal to the specified timeout value. If the time difference exceeds timeout, the later event initiates a new session.
Your task is to calculate and return the total number of distinct sessions across all users.
Input
events: A list of[userId, time]pairs (e.g.,[[1, 10], [2, 5], [1, 12], ...]).timeout: A non-negative integer representing the maximum allowed time difference between consecutive events within the same session.
Output
- An integer representing the total count of sessions across all users.