Top K Frequent Elements After Each Stream Update
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The tie rule is what breaks the first solution you'll write. Salesforce reported this one in September 2026: after every element of a stream, return the top k distinct values, ranked by frequency, with the smaller number winning ties. Example 2 shows it. Stream [5,3,5,3] with k=1 flips between 5 and 3 on every step, because equal counts hand the spot to 3. Miss that and a standard top-k heap passes example 1 and fails the rest. It's a heap or ordered-structure problem with a twist, since you rebuild the answer after every arrival. If you blank during the OA, StealthCoder is the invisible safety net on your screen.
The problem
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 = 2 return = [[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 = 1 return = [[5],[3],[5],[3]] When 3 and 5 tie, 3 is ranked first. Constraints 1 <= stream.length <= 10000 1 <= k <= min(100, stream.length) -1000000000 <= stream[i] <= 1000000000 The total number of returned integers is at most 1000000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Count every value in a hash map. Each arrival bumps exactly one count by one, so the ranking changes in only one place. Keep a list of the current top k sorted by count descending, then value ascending. If the arriving value is already in the list, update it and bubble it toward the front. If it isn't, compare it to the last entry and swap it in when it ranks higher under the same key. That's O(k) per update and roughly 10^6 operations total, which matches the output cap. The pitfalls: a max-heap with lazy deletion piles up stale entries, people forget the value tiebreak in the comparator, and re-sorting every distinct value each step blows up near 10^4 by 10^4. Also return fewer than k items early on. Hand-trace example 2 before submitting. If the comparator slips under pressure, StealthCoder can supply a working version during the live OA without the proctor seeing it.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Top K Frequent Elements After Each Stream Update cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Top K Frequent Elements After Each Stream Update FAQ
What's the trick in Top K Frequent Elements After Each Stream Update?+
Each arrival changes only one value's count by exactly one. So you don't rebuild the ranking from scratch. Keep a sorted top-k list keyed on (count descending, value ascending) and move just the touched value. That turns each update into O(k) work instead of a full sort.
How hard is this really compared to the classic top k frequent problem?+
Harder than it looks. The classic version asks for one answer at the end. This one wants a snapshot after every arrival, plus a strict tie rule. The logic isn't deep, but the comparator and the update path are easy to get subtly wrong.
Why does my solution pass example 1 but fail example 2?+
Example 1 never forces a tie to flip the leader in a way that matters. Example 2 does. With [5,3,5,3] and k=1, equal counts must favor 3. If your comparator only checks frequency, or favors the larger value, you'll output 5 where 3 belongs.
Will a heap with lazy deletion work here?+
It can, but it's the riskier route. Every update pushes a new entry and leaves stale ones behind, so you must validate counts when popping. You also need to pop k valid items and push them back for each snapshot. A maintained sorted list of size k is simpler and fast enough.
How do I prepare in 48 hours for a stream top-k question?+
Write the count map plus sorted top-k list once from scratch. Then hand-trace both examples, especially the tie flip. Check the edge cases: fewer than k distinct values, negative numbers, and k equal to 1. Aim for a solution where the comparator is one clear key function.