Implement k-th largest in a number stream
Company: Zillow
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency with data structures and order-statistics in a streaming context, focusing on API design and time/space complexity trade-offs within the Coding & Algorithms domain.
Constraints
- 1 <= k <= 10^4
- At most 10^4 add calls (len(operations) <= 10^4)
- len(nums) can be 0
- Each integer fits in a signed 32-bit integer (-2^31 .. 2^31 - 1)
- k is always valid: at least k elements exist after each add
Examples
Input: (3, [4, 5, 8, 2], [3, 5, 10, 9, 4])
Expected Output: [4, 5, 5, 8, 8]
Explanation: Start with [4,5,8,2], k=3. add(3): pool {2,3,4,5,8}, 3rd largest=4. add(5): 3rd largest=5. add(10): 3rd largest=5. add(9): pool now has 8,9,10 as top three, 3rd largest=8. add(4): 3rd largest still 8.
Input: (1, [], [5, 3, 8, 1, 10])
Expected Output: [5, 5, 8, 8, 10]
Explanation: Empty start, k=1 tracks the running maximum: 5, then 5, then 8, then 8, then 10.
Input: (2, [0], [-1, -2, -3, 5])
Expected Output: [-1, -1, -1, 0]
Explanation: k=2 with negatives. After add(-1): {-1,0}, 2nd largest=-1. add(-2): {-2,-1,0}, 2nd largest=-1. add(-3): 2nd largest=-1. add(5): pool top two are 5 and 0, 2nd largest=0.
Input: (4, [7, 7, 7, 7, 7], [7, 7])
Expected Output: [7, 7]
Explanation: Duplicates: every element is 7, so the 4th largest is always 7.
Input: (1, [2147483647], [-2147483648])
Expected Output: [2147483647]
Explanation: 32-bit boundary: INT_MAX already present, adding INT_MIN does not change the maximum (k=1).
Hints
- You never need every element sorted at once. To know the k-th largest, you only need to remember the k largest values seen so far.
- A min-heap capped at size k does exactly that: push each new value, and if the heap grows past k, pop the smallest. The root is then the k-th largest.
- Initialize by heapifying nums and trimming it down to k elements before processing any add call.
- Each add is O(log k): one push plus at most one pop. With at most 10^4 adds this is comfortably fast.