Qualifying Run Detector in a Timestamped Event Log
Arrays & HashingAuto-graded · 21 test casesLast updated
Tests whether a candidate can scan a sorted event log in one pass to count maximal same-type runs that meet minimum length and duration thresholds.
You have a log of events recorded in ascending timestamp order. Each entry is a pair [event_type, ts] where event_type is a string or integer and ts is a non-negative integer timestamp.
A run is a maximal contiguous slice of the log where:
- Every entry shares the same
event_type. - For every consecutive pair within the slice, the time difference satisfies
ts[i] - ts[i-1] <= gap_limit.
"Maximal" means you cannot extend the slice in either direction without violating one of the two rules above. Do not count sub-slices inside a run separately.
A run is called a qualifying run when both of the following hold:
- It contains at least
min_countentries. - The span from its first timestamp to its last timestamp is at least
min_span(i.e.,ts[last] - ts[first] >= min_span).
Return the total number of qualifying runs in the log.
Constraints
1 <= len(log) <= 10^5- Timestamps are non-negative integers and are sorted in non-decreasing order (ties are allowed).
gap_limit,min_count, andmin_spanare non-negative integers.- The gap check is inclusive: a gap exactly equal to
gap_limitkeeps the run going. - The span check is inclusive: a span exactly equal to
min_spansatisfies the duration requirement. - A single-entry run has span
0. min_countmay be0or1; treat those as requiring at least that many entries (somin_count = 0is satisfied by every run).
Examples
Example 1
Input: count_super_streak_segments([[1, 0], [1, 2], [1, 5]], 2, 2, 2)
Output: 1
Example 2
Input: count_super_streak_segments([['login', 10], ['login', 12], ['logout', 12], ['logout', 14], ['login', 15]], 2, 2, 2)
Output: 2
Example 3
Input: count_super_streak_segments([['A', 0], ['A', 1], ['A', 2]], 1, 3, 2)
Output: 1
Hints
Hint 1
Walk through the log once and track the current run's type, starting timestamp, ending timestamp, and entry count. When does a run end?
Hint 2
A run breaks when the next entry has a different type OR the gap to the previous timestamp exceeds gap_limit. At that moment, check whether the run just closed meets both thresholds before starting a fresh one.
Hint 3
Don't forget the run still open when you reach the end of the log — it never hits a natural break, so you must evaluate it after the loop finishes.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that you never need to look back: because the log is already sorted by timestamp, a run can only end at the current position. That means a single left-to-right scan with a handful of scalar variables is sufficient.
Algorithm
Keep four pieces of state as you iterate:
run_type— the event type of the active runfirst_ts— timestamp of the first entry in the active runlast_ts— timestamp of the most recent entry seenrun_size— how many entries are in the active run so far
At each entry [event_type, ts]:
- Break check. If there is an active run (
run_size > 0) and eitherevent_type != run_typeorts - last_ts > gap_limit, the active run has closed. Test it againstmin_countandmin_span; if it passes, increment the result counter. Resetrun_sizeto0. - Start or extend. If
run_sizeis now0, open a new run with the current entry. Otherwise, just incrementrun_sizeand updatelast_ts.
After the loop, one run may still be open (the log ends without a natural break). Evaluate it with the same qualification check.
Why it is correct
- Maximality is enforced because we only close a run when we encounter a reason to stop — we never split a valid continuation.
- Inclusive thresholds are handled naturally: the break condition uses strict
>for the gap (ts - last_ts > gap_limit), so a gap of exactlygap_limitdoes not break the run; the span condition uses>=. - Edge cases covered: empty log (loop body never runs, final check skipped because
run_size == 0); single entry (span is 0, satisfiesmin_span = 0); duplicate timestamps (gap is 0, always within any non-negativegap_limit).
Complexity
- Time: O(n) — each entry is visited exactly once.
- Space: O(1) — only scalar variables are maintained regardless of input size.
Reference solution
from typing import List
def count_super_streak_segments(log, gap_limit, min_count, min_span):
count = 0
run_size = 0
first_ts = last_ts = None
run_type = None
def qualifies():
return run_size >= min_count and last_ts - first_ts >= min_span
for event_type, ts in log:
# Current run ends when type changes or gap is too large
if run_size and (event_type != run_type or ts - last_ts > gap_limit):
if qualifies():
count += 1
run_size = 0
if run_size == 0:
run_type = event_type
first_ts = ts
run_size = 1
else:
run_size += 1
last_ts = ts
# Evaluate the final open run
if run_size and qualifies():
count += 1
return count