My first instinct was a max-heap and I said it out loud before realizing that only works if k is fixed ahead of time.
Clarify the requirements: insertions can include duplicates, and findLargest(k) returns the (k+1)-th largest element (0-indexed). Since queries are more frequent, use a data structure that maintains sorted order with O(log n) insertion and O(1) query, such as a balanced BST with subtree sizes or two heaps with lazy deletion. Discuss trade-offs and handle edge cases like k out of bounds.
Pro tip: Mention that you would use a Fenwick tree over compressed values for O(log n) insert and O(log n) query, but if queries are truly O(1) needed, a balanced BST with subtree sizes is optimal. Also, clarify if the data is static or dynamic, as that changes the approach.
Ask about expected number of insertions vs queries, memory constraints, and whether k is guaranteed valid. Confirm that duplicates are allowed and that findLargest(0) returns the maximum.
Suggest a balanced BST (e.g., AVL or Red-Black tree) augmented with subtree sizes, or a Fenwick tree over compressed values. Explain how it supports O(log n) insertion and O(log n) query for the k-th largest.
Compare with alternatives like sorting on insert (O(n) insert, O(1) query) or maintaining a sorted list. Justify why the chosen structure optimizes query time while keeping insertion efficient.
Discuss how duplicates are stored (e.g., counts in nodes) and how to handle k out of bounds (return null or throw exception). Also consider concurrency if needed.
Mention that if queries are extremely frequent, a two-heap approach with lazy deletion can give O(1) query for the median, but for arbitrary k, a BST is better. Also, consider if the data is static, then sorting once and using binary search is optimal.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.