Coding questions · Oracle
Find users with at least one valid session
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:
-
A list of log entries
logs, where each entry is a string in the format:"userId action timestamp"userIdis a non-empty string without spaces (e.g.,"u1").actionis either"signin"or"signout".timestampis an integer representing time in seconds.
-
An integer
maxTime.
A session for a user is defined as a pair of actions:
- A
"signin"log for that user at timet_in, and - The next
"signout"log for the same user at timet_outsuch thatt_out > t_in, - And that
signouthas not already been paired with a previoussigninfor 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:
- The
logslist may not be sorted by time; you should handle that. - Each log belongs to exactly one user, determined by
userId. - If there is a
signoutwithout any earlier unmatchedsigninfor that user, ignore thatsignout. - If there is a
signinwithout any latersignout, it does not form a session. - A single user may have multiple sessions; if any one of them is valid, that user should be included in the output.
- The output can be in any order.
Example
logs = [
"u1 signin 1",
"u2 signin 2",
"u1 signout 5",
"u2 signout 20",
"u1 signin 30",
"u1 signout 35"
]
maxTime = 10
- For
u1:- Session 1: signin at 1, signout at 5 → duration 4 (valid)
- Session 2: signin at 30, signout at 35 → duration 5 (valid)
- For
u2:- Session 1: signin at 2, signout at 20 → duration 18 (invalid if
maxTime = 10)
- Session 1: signin at 2, signout at 20 → duration 18 (invalid if
So the output should include "u1" (has valid sessions) and exclude "u2".
Implement a function that, given logs and maxTime, returns all such userIds.
Users with at Least One Valid Session
Constraints
- Logs may be unsorted
Hints
- Sort by timestamp and pair each signout with the earliest unmatched prior signin for that user.