Find kth element and sliding-window kth in stream
Company: xAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of order statistics and selection in arrays as well as sliding-window stream processing, including maintaining kth elements with bounded-value frequency or bucket counts.
Part 1: Kth Smallest Element in an Array
Constraints
- 1 <= len(nums) <= 200000
- 1 <= k <= len(nums)
- Values may be negative, large, and duplicated
- The input is guaranteed to be valid
Examples
Input: ([7], 1)
Expected Output: 7
Explanation: Single-element edge case: the only element is the 1st smallest.
Input: ([3, 1, 5, 2, 4], 3)
Expected Output: 3
Explanation: Sorted order is [1, 2, 3, 4, 5], so the 3rd smallest is 3.
Input: ([5, 3, 1, 3, 2, 3], 4)
Expected Output: 3
Explanation: Sorted order is [1, 2, 3, 3, 3, 5]. Duplicates count, so the 4th smallest is 3.
Input: ([-10, 1000000000, 0, -10, 5], 5)
Expected Output: 1000000000
Explanation: The 5th smallest is the largest value.
Hints
- You do not need to fully sort the array; think about partitioning around a pivot.
- When many values equal the pivot, grouping values into less-than, equal-to, and greater-than regions avoids repeated work.
Part 2: Kth Smallest Value in Each Time-Based Stream Window
Constraints
- 0 <= len(events) <= 200000
- Events are in non-decreasing timestamp order
- 0 <= W <= 1000000000
- 1 <= k <= 200000
- min_value <= value <= max_value for every event
- 1 <= max_value - min_value + 1 <= 100000
- Timestamps and values are integers
Examples
Input: ([(1, 10), (3, 7), (6, 8)], 5, 2, 0, 10)
Expected Output: [-1, 10, 8]
Explanation: At t=6, the window is 1 < timestamp <= 6, so it contains values [7, 8], whose 2nd smallest is 8.
Input: ([(1, 5), (2, 1), (3, 5), (7, 2), (8, 1)], 5, 2, 1, 5)
Expected Output: [-1, 5, 5, 5, 2]
Explanation: At t=7, events with timestamp <= 2 expire because the condition is timestamp > 2. The remaining values are [5, 2], so the 2nd smallest is 5.
Input: ([], 10, 1, 0, 100)
Expected Output: []
Explanation: Empty stream edge case: there are no events, so there are no outputs.
Input: ([(1, 2), (2, 3)], 10, 3, 0, 5)
Expected Output: [-1, -1]
Explanation: The window never contains at least 3 events.
Input: ([(5, 0), (5, 3), (5, 1)], 1, 2, 0, 3)
Expected Output: [-1, 3, 1]
Explanation: All events have the same timestamp and remain in the window together. After all three values [0, 3, 1] arrive, the 2nd smallest is 1.
Hints
- Because timestamps arrive in sorted order, expired events can be removed from the front of a queue.
- Maintain a frequency count for each possible value, then use cumulative counts to locate the k-th smallest value.