Find kth element and sliding-window kth in stream
Task 1: Kth Element in an Array
Given an integer array nums of length n and an integer k (1-indexed), your task is to return the k-th smallest element in nums.
- Input:
nums: int[],k: int - Output:
int(the k-th smallest value) - Constraints:
1 <= n <= 2e51 <= k <= n- Values in
numsmay be large and can contain duplicates.
Task 2 (Follow-up): Kth Element per Time Window in a Large Data Stream
You will receive a continuous stream of events, each represented as a (timestamp, value) pair. These events arrive in non-decreasing timestamp order. For a fixed window length W (defined in terms of time), for each new event arriving at time t, you must output the k-th smallest value among all events whose timestamps fall within the inclusive window: t - W < timestamp <= t.
Requirements
- The data stream can be extremely large; you cannot store all past events indefinitely.
- Memory is limited; you should only store the data strictly necessary for processing the current window.
- Assume that
valuefalls within a known, bounded range[MIN, MAX]. This implies you can leverage a bucket/count array (frequency table) to store value counts instead of individual values.
Output
For each incoming event, your system should output the k-th smallest value within the current time window. You should also specify the behavior if the window contains fewer than k items (e.g., output null, -1, or no output).
Example
Consider W=5 and k=2. If the events arrive as:
(1, 10)(3, 7)(6, 8)
When the event (6, 8) arrives (at t=6), the active window includes events with timestamps t_event such that 6 - 5 < t_event <= 6, which means 1 < t_event <= 6. The events in this window are (3, 7) and (6, 8). Among these, the values are 7 and 8. The 2nd smallest value is 8.
AI interview prep
Core dilemma
Efficiently maintaining the k-th smallest element in a dynamically changing time-based window of a large data stream, while adhering to strict memory limits and potentially high throughput, especially when the value range [MIN, MAX] might be large.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find kth element and sliding-window kth in stream
Task 1: Kth Element in an Array
Given an integer array nums of length n and an integer k (1-indexed), your task is to return the k-th smallest element in nums.
- Input:
nums: int[],k: int - Output:
int(the k-th smallest value) - Constraints:
1 <= n <= 2e51 <= k <= n- Values in
numsmay be large and can contain duplicates.
Task 2 (Follow-up): Kth Element per Time Window in a Large Data Stream
You will receive a continuous stream of events, each represented as a (timestamp, value) pair. These events arrive in non-decreasing timestamp order. For a fixed window length W (defined in terms of time), for each new event arriving at time t, you must output the k-th smallest value among all events whose timestamps fall within the inclusive window: t - W < timestamp <= t.
Requirements
- The data stream can be extremely large; you cannot store all past events indefinitely.
- Memory is limited; you should only store the data strictly necessary for processing the current window.
- Assume that
valuefalls within a known, bounded range[MIN, MAX]. This implies you can leverage a bucket/count array (frequency table) to store value counts instead of individual values.
Output
For each incoming event, your system should output the k-th smallest value within the current time window. You should also specify the behavior if the window contains fewer than k items (e.g., output null, -1, or no output).
Example
Consider W=5 and k=2. If the events arrive as:
(1, 10)(3, 7)(6, 8)
When the event (6, 8) arrives (at t=6), the active window includes events with timestamps t_event such that 6 - 5 < t_event <= 6, which means 1 < t_event <= 6. The events in this window are (3, 7) and (6, 8). Among these, the values are 7 and 8. The 2nd smallest value is 8.