Count super-streak segments in an event stream
GoogleData Scientist · Technical ScreenLast updated
Count super-streak segments in a time-ordered event stream with an O(n) scan. The solution explains maximal segment boundaries, inclusive gap handling, duration and count checks, edge cases, streaming behavior, and changing-threshold follow-ups.
You are given a time-ordered sequence of events. Each event has:
type, a string or integer event type.ts, an integer timestamp in milliseconds or seconds.
A streak segment is a maximal contiguous subsequence of the input where:
- All events have the same
type. - For every adjacent pair in the segment, the time gap satisfies
ts[i] - ts[i-1] <= T.
A streak segment is a super streak if:
- The number of events in the segment is at least
N. - The duration
ts[last] - ts[first]is at leastX.
Implement a function that returns the number of super streak segments.
Constraints & Assumptions
- Events are already sorted by timestamp ascending.
T,N, andXare non-negative.- The gap threshold is inclusive: a gap exactly equal to
Tcontinues the segment. - A single event has duration 0.
- Count maximal streak segments, not every subarray inside a streak.
Clarifying Questions to Ask
- Can timestamps be equal?
- Can
Nbe 0 or 1, and how should that be interpreted? - Is the input too large to store, or can it be processed as a stream?
- Should the function return only the count or also the segment boundaries?
Part 1 - Define Segment Boundaries
When does a current streak segment end?
What This Part Should Cover
- A segment ends when the next event has a different type or the time gap exceeds
T. - The current segment must be evaluated once at the boundary.
- Maximality means you should not count subsegments separately.
Part 2 - Design The Algorithm
How would you count super streak segments efficiently?
What This Part Should Cover
- One pass through the events.
- Maintain current segment type, first timestamp, last timestamp, and count.
- Evaluate the segment at each break and after the loop.
Part 3 - Analyze Complexity And Edge Cases
What is the time and space complexity, and what edge cases matter?
What This Part Should Cover
O(n)time andO(1)extra space.- Empty input, single event, exact threshold gap, timestamp duplicates,
X = 0,N = 1, and type changes.
Part 4 - Handle Changing Parameters Over Time
How would you adapt the solution if N, T, or X can change over time?
What This Part Should Cover
- If thresholds are attached to each event or time window, segment-breaking rules must use the active
Tat the relevant adjacent gap. - For changing
NorX, evaluate completed segments against the thresholds active for that segment or query. - For many offline threshold queries, precompute segment summaries and answer queries from those summaries rather than rescanning raw events.
Follow-up questions
- How would you return the actual segment ranges?
- What if events are not sorted?
- What if the data arrives as an unbounded stream?
- How would you answer many different `(N, T, X)` queries efficiently?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.