Streaming Top K Frequent Elements
Updated
Problem statement
Given an integer array stream and an integer k, process the values from left to right. After each new value arrives, return the current most frequent distinct values.
- Return at most
kvalues for each prefix. - Order values by decreasing frequency in the prefix.
- When two values have the same frequency, place the smaller value first.
The result contains one row for each arrival, in the same order as stream.
Function
topKAfterEach(stream: int[], k: int) → int[][]Examples
Example 1
stream = [1,2,1,3,2,1]k = 2return = [[1],[1,2],[1,2],[1,2],[1,2],[1,2]]Value 1 leads alone after its second arrival. Later, values 1 and 2 both have frequency 2, and the smaller value comes first. The first row holds only the first value, since nothing else has arrived yet. Every later row lists these same values in this order, which gives the answer.
Example 2
stream = [4,4,5,5,3]k = 1return = [[4],[4],[4],[4],[4]]Each row holds just the single most frequent value. The first value leads alone until the fourth arrival. Then values 4 and 5 are tied, so 4 stays because it is smaller. The last arrival is a new value seen only once, so it does not win. Every row holds that same value, which is the answer.
Constraints
1 <= stream.length <= 10^5.1 <= k <= 10^5.- Every value in
streamis a signed 32-bit integer. - Each row holds as many values as the smaller of
kand the number of distinct values seen so far.
More Amazon Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Document Enrichment Stream AggregatorBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Progressive Banking SystemAirbnb · OAAirbnb interview questions
- Cloud Compute Credits SystemCoinbase · OACoinbase interview questions
- Find Median from Data StreamSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Dynamic Median with Additions and DeletionsByteDance · ONSITE INTERVIEWByteDance interview questions
- Top K With a Deterministically Noisy Comparator (MLE)Mercor · ONSITE INTERVIEWMercor interview questions
- Capacity-Limited Timed CacheNetflix · PHONE SCREENNetflix interview questions