← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round with a sequence-processing problem that looked manageable on the surface but had enough edge cases to keep you busy for a while.

Questions Asked (1)

Q1

Given a sequence of typed events with timestamps, find all segments where the same event type repeats consecutively at least N times, each adjacent pair is within T time units, and the total duration spans at least X. Return the count of such segments.

Algorithms & Data Structures
Author's notes

My first instinct was a basic sliding window and I started coding before fully thinking through what 'segment' meant here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the exact conditions for a valid segment (consecutive same-type events, each adjacent gap ≤ T, total duration ≥ X, and length ≥ N) and confirm whether segments can overlap or must be maximal. Then propose a single-pass linear scan that groups consecutive events of the same type, tracks the start of the current valid run, and counts segments that satisfy all constraints.

Pro tip: Explicitly state your assumptions about overlapping segments and whether the count includes all valid subsegments or only maximal ones—this ambiguity is often the real test. Also, mention that the solution runs in O(n) time and O(1) extra space, which is optimal.

1. Clarify requirements and edge cases

Ask whether segments must be maximal, whether overlapping segments are counted separately, and confirm the definitions of 'consecutively', 'within T', and 'spans at least X'. Discuss edge cases like N=1, X=0, or empty input.

2. Design a linear scan algorithm

Iterate through the events while maintaining the current run of identical event types. For each event, check if it continues the run (same type and gap ≤ T); if not, reset the run.

3. Track segment validity and count

Within a run, maintain a sliding window of events that satisfy the gap constraint. When the window length ≥ N and its total duration ≥ X, count it as a valid segment (or count all valid subsegments if overlapping is allowed).

4. Handle overlapping segments efficiently

If overlapping segments are counted, use two pointers to count all valid subsegments ending at the current event without enumerating them, ensuring O(n) time.

5. Analyze complexity and test

State that the algorithm runs in O(n) time and O(1) space. Walk through a small example to verify correctness, including cases where gaps exceed T or duration is just below X.

Key Points to Mention

  • Clarify whether segments must be maximal or if all valid subsegments are counted.
  • Use a single pass with a sliding window to avoid O(n^2) brute force.
  • Maintain the start index of the current valid run and the last event timestamp.
  • Check both the length constraint (≥ N) and the duration constraint (≥ X) for each candidate segment.
  • Handle edge cases: N=1, X=0, gaps exactly equal to T, and events with identical timestamps.
  • Time complexity O(n) and space complexity O(1) are optimal for this problem.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.