Quant Memo
Coding/●●●●●

k-th largest element in a price stream

Asked at HRT, Citadel

Values arrive one at a time. For a fixed k chosen up front, after each add(x) report the k-th largest value seen so far.

k = 2
add(5) -> (only one value yet)
add(3) -> 3      # 2nd largest of {3, 5}
add(8) -> 5      # 2nd largest of {3, 5, 8}
add(9) -> 8      # 2nd largest of {3, 5, 8, 9}

Support add in O(logk)O(\log k). Re-sorting per tick is O(nlogn)O(n \log n); a full max-heap of everything wastes O(n)O(n) space.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions