Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Kth Largest Element in a Stream

Easy
heapdesignstream

Design a class that tracks the k-th largest element in a stream of test scores.

Input: one operation per line —

  • init k v1 v2 … — first line: initialize with k and the initial values (possibly none)
  • add x — add a score and print the current k-th largest

Output: init prints null; each add prints the k-th largest after insertion. There are always at least k values when add is called.

Example 1
Input:
init 3 4 5 8 2
add 3
add 5
add 10
add 9
add 4
Output:
null
4
5
5
8
8

Constraints

  • 1 ≤ k ≤ 10^4
  • Up to 10^4 add calls.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.