KatInterview

Qualifying Run Detector in a Timestamped Event Log

Arrays & HashingAuto-graded · 21 test cases
EasyAuto-gradedArrays & Hashingarrayssliding-windowsimulation

Last 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:

  1. Every entry shares the same event_type.
  2. 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_count entries.
  • 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, and min_span are non-negative integers.
  • The gap check is inclusive: a gap exactly equal to gap_limit keeps the run going.
  • The span check is inclusive: a span exactly equal to min_span satisfies the duration requirement.
  • A single-entry run has span 0.
  • min_count may be 0 or 1; treat those as requiring at least that many entries (so min_count = 0 is 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.