Fixed-K Kth Largest Stream
Updated
Problem statement
You are given a fixed rank k, an initial array of integers initialValues, and an array additions whose values arrive in order.
After each value in additions is inserted, report the k-th largest value among every value seen so far. Duplicate occurrences occupy separate positions in the ranking.
Return one result for each arriving value, in the same order as additions.
Implement kthLargestAfterEachAdd(k, initialValues, additions).
Function
kthLargestAfterEachAdd(k: int, initialValues: int[], additions: int[]) → int[]Examples
Example 1
k = 3initialValues = [4,5,8,2]additions = [3,5,10,9,4]return = [4,5,5,8,8]After adding 3, the three largest values are 8, 5, 4. Later additions raise the third-largest value first to 5 and then to 8.
Example 2
k = 2initialValues = [5,5]additions = [5,4,6]return = [5,5,5]Repeated values occupy separate ranks. Even after 6 arrives, the second-largest value remains 5.
Example 3
k = 1initialValues = []additions = [-10,-5,-7]return = [-10,-5,-5]With k = 1, each result is the maximum value seen so far.
Constraints
0 <= initialValues.length <= 200000.1 <= additions.length <= 200000.initialValues.length + additions.length <= 200000.1 <= k <= initialValues.length + 1.- Every value fits in a signed 32-bit integer.
More Google Onsite Interview questions
- 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
- Route Pattern MatcherPHONE SCREEN · ONSITE 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
- Top K Largest Values from a StreamThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Fixed-K Kth Largest StreamAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Top K With a Deterministically Noisy Comparator (MLE)Mercor · ONSITE INTERVIEWMercor interview questions
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Meeting Rooms IISalesforce · ONSITE INTERVIEWSalesforce interview questions
- Generate Complete Inputs for a Supplied ClassifierGeneral Motors · ONSITE INTERVIEW
- Minimum Meeting Rooms Without a Sweep LineGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions