FreeCareerPath
Build the PrimitiveEasy

Kth Largest Element in a Stream

Implement `KthLargest(k, nums)` with `add(val)` returning the kth largest value seen so far. Keeping everything sorted is O(n log n) per add. Keep a *min*-heap of exactly the k largest values instead: its root is the kth largest, and anything smaller than the root can be discarded immediately. The counter-intuitive part — a min-heap for a "largest" query — is the question.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Kth Largest Element in a Stream" are saved to your account — logging in takes a few seconds.

Kth Largest Element in a Stream — FreeCareerPath