Reported July 2026
Oracleheap priority queue

Top K Frequent Elements with Larger-Value Tie Break

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

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

The mistake that sinks most first attempts at this Oracle OA, reported in July 2026, is ignoring the tie break. Top K Frequent Elements looks like a warm-up, so people count with a hash map, grab the top k, and submit. Then Example 2 fails, because 4, 2 and 1 all occur twice and the larger values must come first. If you take this in the next day or two, the heap or sort comparator is the whole problem. Get the ordering right and it's easy. If you blank on it live, StealthCoder sits invisibly on your screen as a safety net and hands you the comparator.

The problem

Given an integer array nums and an integer k, return the k most frequent distinct values.
Order the result by decreasing frequency. When two values have the same frequency, the larger value must appear first.

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

Examples
Example 1
nums = [1,1,1,2,2,3]
k = 2
return = [1,2]
Example 2
nums = [4,4,1,1,2,2]
k = 2
return = [4,2]
All three values occur twice, so the larger values 4 and 2 come first.

Constraints
1 <= nums.length <= 200000
-10^9 <= nums[i] <= 10^9
1 <= k <= the number of distinct values in nums

Reported by candidates. Source: FastPrep

Pattern and pitfall

Count frequencies with a hash map. Then order the distinct values by frequency descending, and by value descending when frequencies match. The simplest route is to sort the distinct entries with that comparator and take the first k. That's O(d log d) for d distinct values, fine for 200000 elements. A min-heap of size k works too, but the comparator has to be inverted correctly: the heap root must be the worst candidate, meaning lowest frequency, and on a tie the smaller value. People flip one half and get wrong output on ties. Another pitfall is negative values. Don't assume nonnegative numbers or use array indexing for counts, since values go down to -10^9. Bucket sort by frequency also works, but you still need to sort each bucket by value descending. StealthCoder is the hedge for the live OA if you freeze on the heap direction.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Top K Frequent Elements with Larger-Value Tie Break 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

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

Oracle reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Top K Frequent Elements with Larger-Value Tie Break FAQ

What's the trick in this Oracle top K frequent problem?+

The trick is the tie break. Count with a hash map, then order by frequency descending and by value descending on equal counts. Most people write the standard top K and miss that 4 must come before 2 when both occur twice. Test Example 2 before you submit.

Should I use a heap or just sort?+

Sorting the distinct entries is simpler and safe. It runs in O(d log d), which is fine at 200000 elements. A size-k min-heap is slightly faster when k is small, but it's easier to get the comparator backwards. Pick sort unless you're confident with heaps.

How do I get the heap comparator right?+

Keep a min-heap of size k where the root is the weakest candidate. Weaker means lower frequency, and on equal frequency, the smaller value. Pop the root when the size exceeds k. At the end, reverse the heap contents to get decreasing order.

Do negative numbers break anything?+

Not if you use a hash map. Values range from -10^9 to 10^9, so don't index an array by value. The tie break with larger value first also works normally with negatives, since -1 is larger than -5 and comes first.

How do I prepare for this in 48 hours?+

Write the hash map plus sort version from scratch, then the heap version. Run Example 2 and a case with all equal frequencies. Check k equal to the distinct count and a single-element array. That covers nearly every failure mode for this problem.

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

OA at Oracle?
Invisible during screen share
Get it