Design structure for insert and k-th largest
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates data-structure design and algorithmic efficiency for dynamic order-statistics over a multiset (duplicates allowed), focusing on implementing insert and k-th largest operations and belonging to the Coding & Algorithms domain.
Constraints
- 0 <= number of operations <= 200000
- -10^9 <= inserted value <= 10^9
- Duplicates are allowed and count as separate elements
- Each findLargest query is valid when it appears
Examples
Input: ([('insert', 3), ('insert', 3), ('insert', 2), ('findLargest', 0), ('findLargest', 1), ('findLargest', 2)],)
Expected Output: [3, 3, 2]
Explanation: The multiset is [3, 3, 2]. In descending order it is [3, 3, 2], so the 0-th, 1st, and 2nd largest values are 3, 3, and 2.
Input: ([('insert', -1), ('insert', 5), ('insert', 5), ('insert', 0), ('findLargest', 0), ('findLargest', 1), ('findLargest', 2), ('insert', -3), ('findLargest', 4)],)
Expected Output: [5, 5, 0, -3]
Explanation: Before inserting -3, the descending order is [5, 5, 0, -1], so queries 0, 1, and 2 return 5, 5, and 0. After inserting -3, descending order is [5, 5, 0, -1, -3], so k=4 returns -3.
Input: ([('insert', 7), ('findLargest', 0)],)
Expected Output: [7]
Explanation: With only one element, the largest value is 7.
Input: ([('insert', 10), ('insert', 4), ('insert', 8), ('findLargest', 1), ('insert', 8), ('findLargest', 2), ('findLargest', 3)],)
Expected Output: [8, 8, 4]
Explanation: After the first three inserts, descending order is [10, 8, 4], so k=1 returns 8. After inserting another 8, descending order is [10, 8, 8, 4], so k=2 returns 8 and k=3 returns 4.
Input: ([],)
Expected Output: []
Explanation: No operations means there are no queries to answer.
Hints
- If you store frequencies of values instead of the whole multiset in sorted order, inserts become much cheaper.
- Convert the k-th largest query into an order-statistics query: in ascending order, it is the (current_size - k)-th smallest element.