FastPrepStreaming Top K Frequent Elements
Problems/Streaming Top K Frequent Elements

Streaming Top K Frequent Elements

Amazon logoAmazon● HardFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon phone screen for full-time and new grad roles. The report describes both the fixed top-k task and its follow-up on a stream. It does not say how to order items with the same frequency. Our version is about 96% the same as the reported question.

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 k values 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 stream is a signed 32-bit integer.
  • Each row holds as many values as the smaller of k and 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

Interview reports that mention this question

  • Amazon Experienced Hire OA and Interviews: 21 Experiences

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int[][] topKAfterEach(int[] stream, int k) {
        // write your code here
    }
}
Checking account…