Top K Frequent Values Across Key-Partitioned Shards
Reported by candidates from JP Morgan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Total values capped at 2 * 10^5 across up to 10^4 partitions. That's the line that kills the lazy approach on this JP Morgan OA, reported September 2026. Sorting every distinct value globally works on paper, but the problem nudges you toward a local Top K per partition, then a merge. It's a heap and counting problem wearing a distributed-systems costume. If you blank on the tie-break or the merge step, StealthCoder runs invisibly on your screen during the live OA and gives you a working solution as a safety net.
The problem
A data set is split across several partitions. You are given partitions, where each row contains the integer values stored on one worker, and an integer k. The data is partitioned by key: every occurrence of the same value belongs to exactly one partition. Different partitions therefore have disjoint sets of distinct values. Return the k most frequent values across the entire data set. Order the result by decreasing frequency. If two values have the same frequency, place the smaller numeric value first. A scalable solution should compute a local Top K on each partition and merge only those local candidates into a global Top K. Function topKFrequentByPartition(partitions: int[][], k: int) → int[] Examples Example 1 partitions = [[1,1,1,2],[3,3,4],[5,5,5,5]] k = 3 return = [5,1,3] The frequencies are 5:4, 1:3, 3:2, and 2:1, 4:1. The first three values are [5,1,3]. Example 2 partitions = [[4,4,2,2],[1,1,3],[7]] k = 4 return = [1,2,4,3] Values 1, 2, and 4 each occur twice and are ordered numerically. Values 3 and 7 each occur once, so 3 fills the fourth position. Constraints 1 <= partitions.length <= 10^4 0 <= partitions[i].length The total number of values across all partitions is at most 2 * 10^5. Every distinct value appears in exactly one partition. 1 <= k <= the total number of distinct values.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that values are disjoint across partitions, so frequencies never need to be summed across workers. Count each partition with a hash map, keep a min-heap of size k per partition, then push those survivors into a global min-heap of size k. The comparator is the whole game: higher frequency wins, and on a tie the smaller value wins. So the heap's worst element is lowest frequency, then largest value. Flip that and you fail Example 2. The common pitfall is trimming to k locally with a sloppy comparator and dropping a candidate that should have survived. Another is forgetting that the final output must be sorted, since a heap pops in reverse order. Total work is about N log k. A flat count of everything plus a full sort also passes at this size, and it's a fine fallback. If the heap comparator tangles your head live, StealthCoder is the hedge for the OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Top K Frequent Values Across Key-Partitioned Shards 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as top k frequent elements. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass JP Morgan's OA.
JP Morgan 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 Values Across Key-Partitioned Shards FAQ
What's the trick in the JP Morgan Top K Frequent by partition problem?+
Disjoint values mean no cross-partition frequency merging. Count per partition, keep the top k locally with a heap, then merge those candidates into one global top k. The real work is the comparator: frequency descending, value ascending on ties.
Do I really need heaps, or can I just sort?+
With at most 2 * 10^5 values, counting everything in one hash map and sorting by (-frequency, value) passes fine. The heap version matches the intended scalable design and runs in N log k. Write whichever you can get correct fast.
How do I handle ties in the heap?+
Use a min-heap where the root is the worst candidate. Worst means lowest frequency, and among equal frequencies, the larger value. Pop the root when the size exceeds k. Then reverse or sort the result at the end to get best first.
Why is local Top K safe before merging?+
Each value lives in exactly one partition, so its local frequency is its global frequency. Any value outside a partition's local top k can't make the global top k, because k better values already exist in that same partition.
How should I prepare in 48 hours for this?+
Rehearse one pattern: hash map count, then a size-k heap with a custom comparator. Test it on both examples by hand, especially the tie case in Example 2. Also write the plain sort version as a backup so you always have a working answer.