Session count by user
An access log records one line per API call as "<user> <ts>", where ts is integer Unix seconds, and the file is already in non-decreasing timestamp order. Analytics defines a session per user: a call starts a new session if it is the user's first call, or if more than gap seconds passed since that user's previous call (a difference of exactly gap continues the same session). Return a dictionary mapping each user to their number of sessions.
Implement
session_counts(lines: list[str], gap: int) → dict[str,int]Examples
in
[["alice 100","bob 130","alice 160","alice 400"],120]out{"bob":1,"alice":2}What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 15 min
solution.py
InputExpectedGot
[["alice 100","bob 130","alice 160","alice 400"],120]{"bob":1,"alice":2}not run yetsample