FastPrepAggregate a Large Dataset With GROUP BY

Aggregate a Large Dataset With GROUP BY

Alpaca logoAlpaca● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement the core of SELECT key, SUM(value) ... GROUP BY key. The arrays describe rows in a large dataset: row i has grouping key keys[i] and integer measure values[i].

Return one string key=sum for every distinct key, sorted lexicographically by key. Use wide integer accumulation so group totals do not overflow a 32-bit sum.

Function

groupBySum(keys: String[], values: int[]) → String[]

Examples

Example 1

keys = ["west","east","west","north"]values = [5,7,-2,4]return = ["east=7","north=4","west=3"]

Rows are accumulated by key and formatted in key order.

Example 2

keys = ["a","a","b"]values = [1000000000,1000000000,-3]return = ["a=2000000000","b=-3"]

The same group can contain large and negative values.

Constraints

  • 0 <= keys.length = values.length <= 200000
  • Keys are nonempty strings of length at most 100.
  • -10^9 <= values[i] <= 10^9

More Alpaca problems

See Alpaca hiring insights
public String[] groupBySum(String[] keys, int[] values) {
  // Write your code here.
}
keys["west","east","west","north"]
values[5,7,-2,4]
expected["east=7", "north=4", "west=3"]
Checking account…