Top K Frequent Elements After Each Stream Update
Updated
Problem statement
A conceptually unbounded integer stream is represented by the finite prefix stream. After each arriving value, return the current top k distinct values.
Order each snapshot by decreasing frequency. When frequencies tie, the numerically smaller value comes first. Before k distinct values have appeared, return all distinct values in that order.
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]]The result is observed after every arrival; counts determine rank before the numeric tie rule.
Example 2
stream = [5,3,5,3]k = 1return = [[5],[3],[5],[3]]When 3 and 5 tie, 3 is ranked first.
Constraints
1 <= stream.length <= 100001 <= k <= min(100, stream.length)-1000000000 <= stream[i] <= 1000000000- The total number of returned integers is at most
1000000.
More Salesforce Onsite Interview questions
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
- Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
- Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Top K Upvoted Songs by ArtistAmazon · ONSITE INTERVIEW
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Dynamic Median with Additions and DeletionsByteDance · ONSITE INTERVIEWByteDance interview questions
- Tiered Expiring Item StoreHudson River Trading · PHONE SCREEN · ONSITE INTERVIEWHudson River Trading interview questions
- Thread-Safe LRU Cache with TTLLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Limit Order Book Matching EngineCitadel · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Task Scheduler with DependenciesScale AI · ONSITE INTERVIEWScale AI interview questions
- Trade Volume LeaderboardBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions