← 返回 google 的题目列表Aggregate Website User Activity and Average Session Duration
类型:online_judge
Problem: Aggregate Website User Activity and Average Session Duration
You are given a collection of website user activity records. Each record contains:
user_id: an integer user ID
timestamp: the time when the action occurred, in format YYYY-MM-DD HH:MM:SS
activity: the activity type, such as login, view, purchase, or logout
Complete two tasks:
Count the number of unique users who performed each activity type. Return a mapping: activity -> unique user count.
Calculate the average time spent on the website per user, in seconds.
For this version, define the second task as follows:
A session starts with a user's login activity and ends with that user's next logout activity.
A user may have multiple sessions.
If a login has no matching later logout, ignore that incomplete session.
If a logout has no matching earlier login, ignore that logout.
For each user, first sum all complete session durations. Then compute the average across users who have at least one complete session.
If no user has a complete session, the average duration is 0.0.
The input records are not guaranteed to be sorted by time, so they must be processed by timestamp order.
Input Format
From standard input:
n
user_id timestamp activity
user_id timestamp activity
...
A timestamp contains both date and time, for example:
1 2024-07-26 10:00:00 login
Output Format
Print two lines:
A JSON string for the unique-user count mapping, with keys sorted lexicographically.
The average time spent, formatted with one decimal place.
Constraints
0 <= n <= 100000
1 <= user_id <= 10^9
activity is a non-empty string without spaces
All timestamps are valid
Example
Input:
7
1 2024-07-26 10:00:00 login
2 2024-07-26 10:05:00 login
1 2024-07-26 10:10:00 view
1 2024-07-26 10:15:00 purchase
2 2024-07-26 10:20:00 view
3 2024-07-26 10:25:00 login
1 2024-07-26 10:30:00 logout
Output:
{"login": 3, "logout": 1, "purchase": 1, "view": 2}
1800.0
Explanation: only user 1 has a complete session, from 10:00:00 to 10:30:00, which is 1800 seconds.
Example
Input
7
1 2024-07-26 10:00:00 login
2 2024-07-26 10:05:00 login
1 2024-07-26 10:10:00 view
1 2024-07-26 10:15:00 purchase
2 2024-07-26 10:20:00 view
3 2024-07-26 10:25:00 login
1 2024-07-26 10:30:00 logout
Output
{"login": 3, "logout": 1, "purchase": 1, "view": 2}
1800.0