This question evaluates the ability to parse and process event logs, pair signin/signout events per user, and handle unordered timestamps and per-user state with time-window constraints.
You are given:
logs
, where each entry is a string in the format:
"userId action timestamp"
userId
is a non-empty string without spaces (e.g.,
"u1"
).
action
is either
"signin"
or
"signout"
.
timestamp
is an integer representing time in seconds.
maxTime
.
A session for a user is defined as a pair of actions:
"signin"
log for that user at time
t_in
, and
"signout"
log for the same user at time
t_out
such that
t_out > t_in
,
signout
has not already been paired with a previous
signin
for that user.
A session is valid if:
(t_out - t_in) <= maxTime
Your task is to:
Return the set (or list) of all
userIds that have at least one valid session.
Additional details and assumptions:
logs
list may
not
be sorted by time; you should handle that.
userId
.
signout
without any earlier unmatched
signin
for that user, ignore that
signout
.
signin
without any later
signout
, it does not form a session.
Example
logs = [
"u1 signin 1",
"u2 signin 2",
"u1 signout 5",
"u2 signout 20",
"u1 signin 30",
"u1 signout 35"
]
maxTime = 10
u1
:
u2
:
maxTime = 10
)
So the output should include "u1" (has valid sessions) and exclude "u2".
Implement a function that, given logs and maxTime, returns all such userIds.