Top K Frequent Elements

Reported by candidates from Ericsson's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Ericsson OA. Under 2s to a working solution.
Founder's read

Ericsson's May 2026 OA includes a heap-priority-queue problem disguised as a frequency counter. You're given an array and asked to return the k most frequent elements, sorted by frequency descending, with ties broken by numeric value. This is a classic two-step: count frequencies, then extract the top k. The trap is the tie-breaking rule. Most candidates forget it or implement it wrong. StealthCoder will spot the pattern and the exact comparator you need the moment you see the problem text.

The problem

Given an integer array nums and an integer k, return the k most frequent elements. To make the output deterministic, sort elements by frequency in descending order. If two elements have the same frequency, the smaller numeric value comes first. Function Description Complete solveTopKFrequentElements. It has the following parameters: int[] nums: the input array int k: the number of elements to return Return an int[] containing the top k elements in the deterministic order described above. 1 appears 3 times and 2 appears 2 times, so they are the two most frequent elements. Every number appears once, so ties are resolved by smaller value first. 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 1 <= k <= number of distinct elements in nums

Reported by candidates. Source: FastPrep

Pattern and pitfall

The algorithm: hash map to count frequencies in one pass, then a min-heap of size k to track the top k elements. The heap comparison is not just frequency; it's frequency descending, then value ascending for ties. Many candidates build a max-heap and pop k times, which works but is less efficient. The correct approach keeps a min-heap of exactly k elements, comparing by (frequency, then by value). When you exceed k elements, pop the smallest. The tie-breaking logic is where most blanks happen under time pressure. StealthCoder reads your problem statement and generates the exact comparator needed, so you can code with confidence even if the pressure makes you second-guess the sort order.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Top K Frequent Elements 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as top k frequent elements. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Ericsson's OA.

Ericsson reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Top K Frequent Elements FAQ

Do I need to use a heap, or can I sort the frequency map?+

You can sort by frequency and value, which is simpler to code fast. Build a list of (frequency, value) tuples, sort by -frequency then by value, and return the first k values. It's O(n log n) instead of O(n log k), but for the constraints here, it's fine and less error-prone.

What's the trick with tie-breaking?+

If two numbers have the same frequency, the smaller number comes first in the output. Your comparator must check frequency first, then value. If you get this backward, you'll fail all the edge cases where ties exist. Double-check the problem statement the moment you code it.

How long will this take to code?+

If you're comfortable with hash maps and sorting, 5-10 minutes. If you're doing a heap, 15 minutes. The logic is straightforward; the risk is the comparator. Read the tie-breaking rule twice before you code the sort.

Will Ericsson ask this again, or is it a one-off?+

Top K Frequent is a staple. Expect it in different forms: top k words, top k pairs, top k anything. The pattern is always count, extract top k, resolve ties. It's been asked for years and shows no signs of stopping.

What if I blank on the comparator?+

Start with a plain sort by frequency descending. Get that working. Then add the tie-breaker as a secondary sort key. Sorting libraries let you chain comparators. If you're truly stuck, a simple O(n log n) sort by both rules beats a buggy heap implementation.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Ericsson.

OA at Ericsson?
Invisible during screen share
Get it