Visit sessions
MediumOnline Assessment
Group the page views of a website into visits, and report the visits that were long enough to matter.
Each event is a string "<user> <ts>": ts is the time of a page view in whole seconds since the start of day 0, so day d covers ts from d * 86400 to d * 86400 + 86399. Events are in no particular order, and a user may have several events with the same ts.
Look at the events of each user in time order. An event starts a new session when any of these holds:
- it is the user's first event;
- it is more than 1800 seconds after the user's previous event (a gap of exactly 1800 keeps the session going);
- it is on a different day than the user's previous event (sessions are cut at midnight, however short the gap).
Otherwise it belongs to the same session as the previous event. The gap is always measured from the previous event, not from the start of the session.
A session is reported only if it contains at least min_events events (events with equal ts each count).
Input: events, a list of strings, and min_events, an integer.
Output: a list with one entry per reported session, [user, start, end, count]: the user, the ts of its first and of its last event, and its number of events. Order the entries by start, and sessions with the same start by user in plain ascending order. If there are none, return [].
Example 1
- Input:
- [["alice 100","alice 1900","bob 50","alice 3701"],1]
- Output:
- [["bob",50,50,1],["alice",100,1900,2],["alice",3701,3701,1]]
- Explanation
- alice's events at 100 and 1900 are exactly 1800 seconds apart, which is still the same session; 3701 is 1801 seconds after 1900, so it opens a new one. bob has a single event, a session of length 0 that is kept because min_events is 1. Sessions are ordered by start time.
Example 2
- Input:
- [["carol 86390","carol 86400","carol 86500","carol 86300"],1]
- Output:
- [["carol",86300,86390,2],["carol",86400,86500,2]]
- Explanation
- carol is active across midnight: 86390 is on day 0 and 86400 is the first second of day 1, so the session is cut there even though the gap is only 10 seconds. The new session then keeps going with 86500.
Example 3
- Input:
- [["dave 4500","eve 0","dave 1500","dave 0","eve 2000","dave 3000"],2]
- Output:
- [["dave",0,4500,4]]
- Explanation
- Events are not sorted in the input. dave's gaps are 1500 seconds each, so every event extends the session and it lasts from 0 to 4500; the 30 minutes are counted from the previous event, not from the session start. eve has two events 2000 seconds apart, giving two sessions of one event each, and both are dropped because min_events is 2.
Constraints
0 <= events.length <= 400;1 <= min_events <= 10- Every event is
"<user> <ts>":userhas no spaces and0 <= ts < 604800(seven days)