Fixed-K Kth Largest Stream
Updated
Problem statement
You are given a fixed rank k, a non-empty starting catalog of integers initialValues, and an array additions of values that arrive later, one at a time.
Each arriving value is added to the catalog. After each one, report the kth largest value among all values seen so far. Each copy of a repeated value takes its own place in the ranking.
Return one result per arriving value, in the order they arrive. You only need to keep enough information to answer these questions for the fixed rank. You do not need to store the whole catalog in sorted order.
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 3 is added, the three largest values are 8, 5, and 4, so the first result is the smallest of them. The second arrival lifts the third-largest value to 5, and the much larger third arrival keeps it there. The fourth arrival lifts it to 8. The last arrival is too small to change it, so the final result is also this value.
Example 2
k = 2initialValues = [5,5]additions = [5,4,6]return = [5,5,5]Each copy of 5 takes its own place in the ranking, so the second-largest value starts as that value. Another copy and then a smaller value arrive, and the second-largest value does not change. Even after 6 arrives, the second-largest value is still 5, so every result is this value.
Example 3
k = 1initialValues = [-10]additions = [-5,-7]return = [-5,-5]With k = 1, each result is the largest value seen so far. The first arrival is larger than the starting value, so it becomes the largest. The second arrival is smaller than that, so both results equal the first arrival.
Constraints
1 ≤ k ≤ initialValues.length ≤ 200000.1 ≤ additions.length ≤ 200000.initialValues.length + additions.length ≤ 200000.- Every value is a signed 32-bit integer.
More Amazon Web Services Onsite Interview questions
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 StreamGoogle · ONSITE INTERVIEWGoogle interview questions
- Top K With a Deterministically Noisy Comparator (MLE)Mercor · ONSITE INTERVIEWMercor interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- 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