Design a class that tracks the kth largest value in a growing integer stream

Quick Overview

Design a class that is initialized with an integer k and a list of numbers, then returns the kth largest value, duplicates included, each time a new number is added to the stream. It tests picking a data structure that keeps every call cheap as the stream grows and reasoning about which values can safely be forgotten.

Design a class that tracks the kth largest value in a growing integer stream

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Design a class that finds the kth largest element in a stream of integers. The kth largest is taken from the sorted order of every element in the stream, repeats included, so duplicates count as separate elements. Implement the `KthLargest` class: - `KthLargest(k, nums)` initializes the object with the integer `k` and the initial stream of integers `nums`. - `add(val)` appends the integer `val` to the stream and returns the kth largest element of the stream after the append. ### Function Signature ```python class KthLargest: def __init__(self, k: int, nums: list[int]) -> None: ... def add(self, val: int) -> int: ... ``` ### Rules - A test is a sequence of calls. `operations[i]` names the call and `arguments[i]` holds its arguments: the constructor receives `[k, nums]` and each `add` receives `[val]`. - The expected output has one entry per call: `None` for the constructor, then the value returned by each `add`. - Duplicates are counted. For the stream `[5, 5, 4]` with `k = 2`, the answer is `5`. ### Constraints - `1 <= k <= 10^4` - `0 <= len(nums) <= 10^4` - `-10^4 <= nums[i] <= 10^4` - `-10^4 <= val <= 10^4` - At most `10^4` calls are made to `add`. - The stream is guaranteed to hold at least `k` elements whenever `add` returns its answer. ### Examples ```text Input: operations = ["KthLargest", "add", "add", "add", "add", "add"] arguments = [[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]] Output: [None, 4, 5, 5, 8, 8] ``` With `k = 3`, the stream after `add(3)` is `[4, 5, 8, 2, 3]`, whose third largest value is 4, and after `add(5)` the three largest are 8, 5 and 5. Once 10 and 9 arrive the three largest are 10, 9 and 8, so the last two calls return 8. ```text Input: operations = ["KthLargest", "add", "add", "add", "add"] arguments = [[1, []], [1], [2], [3], [4]] Output: [None, 1, 2, 3, 4] ``` With `k = 1` the answer is the maximum so far, and every added value is a new maximum. ```text Input: operations = ["KthLargest", "add", "add", "add", "add", "add"] arguments = [[2, [0]], [-1], [1], [-2], [-4], [3]] Output: [None, -1, 0, 0, 0, 1] ``` The first `add` brings the stream to two elements, so it returns the smaller one, -1. After `add(1)` the second largest is 0, adding -2 and -4 does not change it, and adding 3 makes 1 the second largest.

Overview: Design a class that is initialized with an integer k and a list of numbers, then returns the kth largest value, duplicates included, each time a new number is added to the stream. It tests picking a data structure that keeps every call cheap as the stream grows and reasoning about which values can safely be forgotten.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Design a class that finds the kth largest element in a stream of integers. The kth largest is taken from the sorted order of every element in the stream, repeats included, so duplicates count as separate elements.

Implement the KthLargest class:

  • KthLargest(k, nums) initializes the object with the integer k and the initial stream of integers nums .
  • add(val) appends the integer val to the stream and returns the kth largest element of the stream after the append.

Function Signature

class KthLargest:
    def __init__(self, k: int, nums: list[int]) -> None:
        ...

    def add(self, val: int) -> int:
        ...

Rules

  • A test is a sequence of calls. operations[i] names the call and arguments[i] holds its arguments: the constructor receives [k, nums] and each add receives [val] .
  • The expected output has one entry per call: None for the constructor, then the value returned by each add .
  • Duplicates are counted. For the stream [5, 5, 4] with k = 2 , the answer is 5 .

Constraints

  • 1 <= k <= 10^4
  • 0 <= len(nums) <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • -10^4 <= val <= 10^4
  • At most 10^4 calls are made to add .
  • The stream is guaranteed to hold at least k elements whenever add returns its answer.

Examples

Input:
operations = ["KthLargest", "add", "add", "add", "add", "add"]
arguments = [[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output: [None, 4, 5, 5, 8, 8]

With k = 3, the stream after add(3) is [4, 5, 8, 2, 3], whose third largest value is 4, and after add(5) the three largest are 8, 5 and 5. Once 10 and 9 arrive the three largest are 10, 9 and 8, so the last two calls return 8.

Input:
operations = ["KthLargest", "add", "add", "add", "add"]
arguments = [[1, []], [1], [2], [3], [4]]
Output: [None, 1, 2, 3, 4]

With k = 1 the answer is the maximum so far, and every added value is a new maximum.

Input:
operations = ["KthLargest", "add", "add", "add", "add", "add"]
arguments = [[2, [0]], [-1], [1], [-2], [-4], [3]]
Output: [None, -1, 0, 0, 0, 1]

The first add brings the stream to two elements, so it returns the smaller one, -1. After add(1) the second largest is 0, adding -2 and -4 does not change it, and adding 3 makes 1 the second largest.