Kth Largest Element in Stream Problem
Kth Largest Element in Stream Problem — ExecCode Medium DSA Practice
Solve the Kth Largest Element in Stream problem on ExecCode. Free online medium DSA practice in Heap. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Description You are part of a university admissions office and need to keep track of the kth highest test score from applicants in real-time. This helps to determine cut-off marks for interviews and admissions dynamically as new applicants submit their scores. You are tasked to implement a class which, for a given integer k, maintains a stream of test scores and continuously returns the kth highest test score after a new score has been submitted. More specifically, we are looking for the kth highest score in the sorted list of all scores. Implement the KthLargest class: KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of test scores nums. int add(int val) Adds a new test score val to the stream and returns the element representing the k^th largest element in the pool of test scores so far. Constraints 0 <= nums.length <= 10^4 1 <= k <= nums.length + 1 -10^4 <= nums[i] <= 10^4 -10^4 <= val <= 10^4 At most 10^4 calls will be made to add.
Examples
Input {"k": 3, "nums": [4, 5, 8, 2]}; Output []. Input {"data": "KthLargest(3, [4,5,8,2]); add(3); add(5); add(10)"}; Output [4,5,5]. Input {"data": "KthLargest(3, [4,5,8,2]); add(3); add(5); add(10); add(9); add(4)"}; Output [4,5,5,8,8]
Constraints
0 <= nums.length <= 10^4 1 <= k <= nums.length + 1 -10^4 <= nums[i] <= 10^4 -10^4 <= val <= 10^4 At most 10^4 calls will be made to add.
Practice Kth Largest Element in Stream free on ExecCode. Browse DSA problems, topic map, and placement guides.