Reported September 2026
Capgeminiheap priority queue

Top K Frequent Elements

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

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

Capgemini reported this one in September 2026, and the detail that matters is the tie-break: equal frequencies list the smaller value first. Example 2 shows it, with -1 coming before 4 even though both appear twice. It's Top K Frequent Elements with an ordering rule bolted on, so the heap or bucket idea you already know still works. You just have to sort the output correctly. If the OA is in a day or two, learn the count-then-select pattern cold. StealthCoder is the safety net if you blank on the live assessment, running invisibly while you work.

The problem

You are given an integer array nums and an integer k. Return the k distinct values that occur most frequently.
The selected set is guaranteed to be unique. For deterministic output, list selected values by decreasing frequency; when two selected values have the same frequency, list the smaller value first.

Function
topKFrequent(nums: int[], k: int) → int[]

Examples
Example 1
nums = [1,1,1,2,2,3]
k = 2
return = [1,2]
The frequencies are three for 1, two for 2, and one for 3.
Example 2
nums = [4,4,-1,-1,2]
k = 2
return = [-1,4]
Both selected values occur twice, so the smaller value comes first.

Constraints
1 <= nums.length <= 100000
-10000 <= nums[i] <= 10000
1 <= k <= the number of distinct values in nums.
The set of k most frequent values is unique.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Count occurrences with a hash map. Then pick the top k by frequency. A min-heap of size k gives O(n log k), but the ordering rule trips people up. Compare by frequency first, then by value, and sort the final k by decreasing frequency and ascending value. The simplest safe approach: build the list of (value, count) pairs, sort by (-count, value), and slice the first k. With values bounded to -10000..10000, there are at most 20001 distinct values, so the sort is cheap. Bucket sort by frequency works too. The common pitfall is returning heap order, which is backwards, or forgetting the tie-break so ties come out in insertion order. Negative values also break any array indexing by value unless you offset by 10000. If you freeze during the live OA, StealthCoder can hand you the sorted-pairs solution, but you can write it in five lines.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

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 Capgemini's OA.

Capgemini reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Top K Frequent Elements FAQ

What's the trick in Top K Frequent Elements?+

Count with a hash map, then select the k largest by frequency. A min-heap of size k or a plain sort both work. The Capgemini twist is the output order: decreasing frequency, and the smaller value first on ties. Handle that in your comparator and you're done.

How hard is this problem really?+

Easy to medium. The core idea is standard. The risk is small mistakes: wrong tie-break, returning heap order, or mishandling negative numbers. With n up to 100000 and values bounded, even a sort-based solution is fast enough.

Should I use a heap or just sort?+

Sorting the distinct (value, count) pairs by (-count, value) is shorter and less bug-prone. A heap only helps when distinct values are huge. Here there are at most 20001 distinct values, so sorting is fine. Use the heap if you want to show the O(n log k) approach.

How do I handle the tie-break correctly?+

Sort key is count descending, then value ascending. In Example 2, -1 and 4 both occur twice, so -1 goes first. If you use a heap, make sure its comparator encodes the same rule, then sort the final k results again to be safe.

How do I prepare for this in 48 hours?+

Write it three times from scratch: hash map count, sorted pairs, then a bucket-by-frequency version. Test both examples, including the tie case with negative numbers. Also check k equals the number of distinct values. That covers nearly every edge case in this problem.

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

OA at Capgemini?
Invisible during screen share
Get it