FastPrepTop-K Values per Key in MapReduce
Problems/Top-K Values per Key in MapReduce

Top-K Values per Key in MapReduce

Databricks logoDatabricks● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Databricks phone screen. Our version is about 90% the same as the reported question, which picks values for each key. After some mixed wording at the start, the interviewer made clear how values are chosen, that grouping is automatic, and that the mapper just passes data through. We chose the finite input, the integer range, how duplicates work, and a fixed output order.

Return the result of a MapReduce reducer. The mapper passes each key-value pair through unchanged, and the shuffle stage groups the pairs by key. Find the k largest values for each key separately, counting repeated values.

For this exercise, assume the two arrays keys and values represent the complete ordered input stream: position i contains pair (keys[i], values[i]). Process every pair. Use the supplied arrays; no distributed service or network request is needed.

For each distinct key, keep its min(k, occurrenceCount) largest integer values. Count repeated values separately. Return rows [key, decimalValue], with keys in ascending lexicographic order and each key’s selected values in descending numeric order. The decimal value uses the ordinary integer representation with no leading zeroes except 0 itself. An empty stream returns an empty array.

The operation is per-key selection; do not count hits or choose one global top-k list.

Function

topKPerKey(keys: String[], values: int[], k: int) → String[][]

Examples

Example 1

keys = ["b","a","b","a","c"]values = [7,1,4,9,3]k = 2return = [["a","9"],["a","1"],["b","7"],["b","4"],["c","3"]]

Return a:9,1 then b:7,4 then c:3. Selection occurs independently within each key.

Example 2

keys = ["x","x","x","x"]values = [5,5,2,5]k = 2return = [["x","5"],["x","5"]]

The two retained values are both 5; do not deduplicate the stream.

Example 3

keys = []values = []k = 3return = []

No key occurs and the output is empty.

Constraints

  • 0 <= keys.length = values.length <= 200.
  • Every key contains from 1 through 10 lowercase English letters.
  • -10^6 <= values[i] <= 10^6.
  • 1 <= k <= 20.
  • Identical keys and identical values may occur repeatedly; preserve their multiplicity.

More Databricks Phone Screen questions

  • Rolling Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Minimize CommuteOA · PHONE SCREEN · Seen Oct 2026
  • Customer Revenue with Direct ReferralsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • File Encryption Tree OptimizationPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Encrypted File Count and Minimum TimePHONE SCREEN · Seen Sep 2026
  • Circuit Breaker Database FailoverPHONE SCREEN · Seen Aug 2026
  • Find First Anagram IndexPHONE SCREEN · Seen Aug 2026
  • Ordered IP FirewallPHONE SCREEN · Seen Aug 2026
  • Key-Value Store GET and PUT QPSPHONE SCREEN · Seen Aug 2026
  • Snapshot Set IteratorPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 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 IP Addresses Across Log FilesRender · PHONE SCREEN
  • Top K Frequent Values Across Key-Partitioned ShardsJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
  • 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

Databricks interview experiences

  • Databricks Senior SWE Onsite AI Rounds: 1 Experience
All Databricks interview questionsSee Databricks hiring insights
Saved
class Solution {
    public String[][] topKPerKey(String[] keys, int[] values, int k) {
        // write your code here
    }
}
Checking account…