Dynamic K-th Largest Queries
Updated
Problem statement
Process an ordered sequence of operations against an initially empty multiset of integers.
INSERT valueinserts one occurrence ofvalue.FIND kreturns the zero-indexedkth-largest value currently stored.
Duplicate values occupy separate ranks. Every FIND operation uses a valid k. Return the results of all FIND operations in query order.
Design the query operation to be substantially faster than sorting the entire multiset after every insertion.
Function
processKthLargest(operations: String[][]) → int[]Examples
Example 1
operations = [["INSERT","3"],["INSERT","3"],["INSERT","2"],["FIND","0"],["FIND","1"],["FIND","2"]]return = [3,3,2]The sorted multiset is [3,3,2], and duplicates occupy the first two ranks.
Example 2
operations = [["INSERT","-1"],["INSERT","5"],["FIND","1"],["INSERT","10"],["FIND","1"]]return = [-1,5]Before inserting 10, the second-largest value is -1. Afterward it is 5.
Constraints
1 <= operations.length <= 100000.- Every operation has the required operand and is either
INSERTorFIND. - Inserted values fit in a signed 32-bit integer.
- Every
FIND ksatisfies0 <= k < current multiset size.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Count Later Lower ReadingsRippling · ONSITE INTERVIEW
- Maintain a Consistent-Hashing RingCohesity · ONSITE INTERVIEW
- Transaction Event StoreCapital One · ONSITE INTERVIEW
- Employee Ratings Management SystemAmazon · PHONE SCREEN
- Running Medians and PercentilesStackAdapt · PHONE SCREEN
- Range ModuleWalmart · PHONE SCREEN
- Order Sequence Prefix Counts and QuantitiesHeadlands Technologies · PHONE SCREEN
- Top K Pins with Insert and QueryPinterest · PHONE SCREEN
- Maintain a Consistent-Hashing RingApple · PHONE SCREEN
- Structured Log Stream with QueriesOpenAI · PHONE SCREEN