Code RoomKth largest element stream
EasyPrep Room Coding #4517

Kth largest element stream

CodingDistributed systemsAlgorithms & data structuresEntry–Mid~15 min

Maintain the k-th largest element in a stream. Process ops of the form ['add', x]; after each add, if at least k elements have been seen, report the current k-th largest value, otherwise report -1. Return the list of reported values, one per 'add'. k >= 1.

Implement
kth_largest_stream(k: int, ops: list[list]) → list[int]
Examples
in[3,[["add",4],["add",5],["add",8],["add",2],["add",3],["add",5],["add",10],["add",9],["add",4]]]out[-1,-1,4,4,4,5,5,8,8]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 15 min
InputExpectedGot
[3,[["add",4],["add",5],["add",8],["add",2],["add",3],["add",5],["add",10],["add",9],["add",4]]][-1,-1,4,4,4,5,5,8,8]not run yetsample