Kth Largest Element in a Stream

Hard Problems DSA practice problem on Onlearn.

Difficulty: easy.

Topics: How can we efficiently find the Kth largest element in a stream of running integers?, Priority Queue, Heap/Priority Queue, Stream Processing, Sorting, Brute Force, priority queue, heap, algorithm design, time complexity analysis, data structure operations, Heap (Priority Queue), Streaming Data / Running Median.

Problem Statement: Kth Largest Element in a Stream Design a class KthLargest that efficiently maintains and retrieves the Kth largest element from a stream of integers. The stream can have elements added dynamically, and the Kth largest element must be retrievable at any point. Input/Initialization: The constructor KthLargest(int k, vector<int & nums) initializes the object with the integer k and the initial elements of the stream. The method add(int val) appends the integer val to the stream and returns the current Kth largest element after the addition. Output: The method add should return the Kth largest element in the stream after inserting the new value. Constraints: 1 ≤ k ≤ 10^4 0 ≤ nums.length ≤ 10^4 10^4 ≤ nums[i] ≤ 10^4 At most 10^4 calls will be made to add. It is guaranteed there will be at least k elements in the stream when searching for the Kth largest element. Example: Difficulty: Medium Related DSA Concepts: Priority Queue, Heap/Priority Queue, Stream Processing, Sorting