Maintain k-th largest in a stream
GoogleSoftware Engineer · OnsiteLast updated
This question evaluates understanding of data structures and streaming algorithms for maintaining k-th order statistics under dynamic inserts, emphasizing time-space trade-offs and correct handling of duplicates.
Design and implement a class KthLargest that, given an integer k and an initial list of integers, supports: (
- KthLargest(k, nums): constructor; (
- add(val): adds val to the data structure and returns the current k-th largest element. Achieve O(log k) time per insertion and O(k) space. Describe your data structures, handle duplicates, and analyze complexity.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.