Detect k events in a sliding window
Problem Description
Implement an algorithm that, for each user, determines whether there exists a set of at least k ACH credit events from distinct device_ids within any sliding window of t minutes, where each amount_cents is greater than or equal to X. If such a window is found, output the earliest [start_ts, end_ts] window and the k associated device_ids; otherwise, output none.
Input
events: An iterable of tuples, each containing(user_id INT, device_id TEXT, ts ISO-8601 string, amount_cents INT). The total number of eventsncan be up to1e6across all users. Events may arrive out of order by up to5minutes.
Parameters
k: An integer representing the minimum number of distinctdevice_idsrequired,k ∈ {3, 4, 5}.t: An integer representing the duration of the sliding window in minutes,tup to1440.X: An integer representing the minimumamount_centsfor an event to be considered,X ≥ 0.
Requirements
- Time Complexity:
O(n log n)overall. - Space Complexity:
O(m)wheremis the maximum number of events for a single user within at-minute horizon. - Handle out-of-order events with a skew of up to
5minutes. - Deduplicate exact duplicate events.
- Timestamps are UTC; be aware of DST transitions and clock skew.
- Return all qualifying users with their earliest window and corresponding
kdevice IDs.
Follow-ups
- Describe a streaming approach (bounded memory) if events arrive per user partition.
- Prove correctness of your window advancement and distinct-device counting.
- Discuss worst-case behavior on highly bursty users and how to mitigate.
AI interview prep
Core dilemma
Balancing efficient per-user sliding window processing with distinctness requirements and out-of-order event handling under tight time/space complexity constraints.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Detect k events in a sliding window
Problem Description
Implement an algorithm that, for each user, determines whether there exists a set of at least k ACH credit events from distinct device_ids within any sliding window of t minutes, where each amount_cents is greater than or equal to X. If such a window is found, output the earliest [start_ts, end_ts] window and the k associated device_ids; otherwise, output none.
Input
events: An iterable of tuples, each containing(user_id INT, device_id TEXT, ts ISO-8601 string, amount_cents INT). The total number of eventsncan be up to1e6across all users. Events may arrive out of order by up to5minutes.
Parameters
k: An integer representing the minimum number of distinctdevice_idsrequired,k ∈ {3, 4, 5}.t: An integer representing the duration of the sliding window in minutes,tup to1440.X: An integer representing the minimumamount_centsfor an event to be considered,X ≥ 0.
Requirements
- Time Complexity:
O(n log n)overall. - Space Complexity:
O(m)wheremis the maximum number of events for a single user within at-minute horizon. - Handle out-of-order events with a skew of up to
5minutes. - Deduplicate exact duplicate events.
- Timestamps are UTC; be aware of DST transitions and clock skew.
- Return all qualifying users with their earliest window and corresponding
kdevice IDs.
Follow-ups
- Describe a streaming approach (bounded memory) if events arrive per user partition.
- Prove correctness of your window advancement and distinct-device counting.
- Discuss worst-case behavior on highly bursty users and how to mitigate.