Top K Frequent Values Across Key-Partitioned Shards
Updated
Problem statement
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 = 3return = [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 = 4return = [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^40 <= 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.
More JP Morgan Chase interview questions
- Longest Consecutive SequencePHONE SCREEN · Seen Nov 2026
- Maximize Binary XOR with a Set-Bit LimitPHONE SCREEN · Seen Oct 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Aug 2026
- All Index Pairs Sum to KPHONE SCREEN · Seen Aug 2026
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
Similar questions at other companies
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
- Top-K Values per Key in MapReduceDatabricks · PHONE SCREENDatabricks interview questions
- Top-K IP Addresses Across Log FilesRender · PHONE SCREEN
- Top-K IP Addresses from File RecordsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Top K Frequent ElementsApple · PHONE SCREENApple interview questions
- Sort Characters by How Often They AppearAdobe · PHONE SCREENAdobe interview questions
- Top Trade Events by NotionalHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
JP Morgan Chase interview experiences
- JPMorgan Chase Experienced SWE Java Interview: 1 Experience
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)