Easy · Streams & I/O

Find Kth Largest in Stream

Given an integer k ≥ 1 and an initial list of integers nums, build a stream object whose add(val) appends val and returns the kth largest value seen so far (duplicates count separately); at least k values are guaranteed to have been seen whenever add returns.

Examples

Example 1

k=3, init=[4,5], stream=[8,2,9]

Output: [4, 4, 5]

Example 2

k=2, init=[3], stream=[1,4,1,5]

Output: [1, 3, 3, 4]

Rebuild it in the studio

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Streams & I/O problems