Implement Table Aggregation
Company: Notion
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to perform data aggregation and manipulate basic data structures by grouping records by key and combining numeric values.
Constraints
- 0 <= number of rows <= 10^5
- Each row is a [key, value] pair.
- key is a non-empty string.
- value is an integer that may be negative, zero, or positive.
- Sums fit in a 64-bit integer.
Examples
Input: [["A", 3], ["B", 5], ["A", 2], ["B", 1], ["C", 4]]
Expected Output: [["A", 5], ["B", 6], ["C", 4]]
Explanation: A: 3+2=5, B: 5+1=6, C: 4. Sorted by key.
Input: []
Expected Output: []
Explanation: Empty table aggregates to an empty result.
Input: [["X", 10]]
Expected Output: [["X", 10]]
Explanation: A single row passes through as its own group.
Input: [["A", -3], ["A", 3], ["B", -5]]
Expected Output: [["A", 0], ["B", -5]]
Explanation: Negative values are summed normally; A's values cancel to 0.
Input: [["z", 1], ["a", 1], ["m", 2], ["a", 4]]
Expected Output: [["a", 5], ["m", 2], ["z", 1]]
Explanation: Insertion order is z, a, m but output is sorted ascending by key; a accumulates 1+4=5.
Hints
- Use a hash map keyed by the group string, adding each row's value into the running total for that key.
- Collect the distinct keys and sort them so the output order is deterministic.
- For the MAX follow-up, replace the '+=' accumulation with a reducer function (e.g. max) so the same loop supports any associative aggregation. For the out-of-memory follow-up, stream the rows and aggregate incrementally, or partition by key and aggregate each partition (map-reduce).