← Questions
Table aggregation with group-by sum
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Notion's technical phone screen, last asked Apr 2026. It tests aggregation, sql and tables.
A table arrives as a list of rows, each a group key and an integer value, as a database table view would hold. Group the rows by key and return each key with the sum of its values.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
aggregate(rows: list[tuple[str, int]]) -> list[tuple[str, int]]— one(key, total)per distinct key, sorted by key so the output is deterministic.
Example
aggregate([("A", 3), ("B", 5), ("A", 2), ("B", 1), ("C", 4)])
-> [("A", 5), ("B", 6), ("C", 4)]
aggregate([]) -> []
Constraints
- Up to 10^5 rows. Keys are non-empty strings; values may be negative or zero. Sums fit in 64 bits.
Clarifying questions
- Does the output order matter, or may it be any order?
- Can a row have a missing key or value, and how should it be treated?
- Will other aggregations be needed later (max, min, count, average)?
- Will the table always fit in memory?
What a strong answer covers
- A single pass with a hash map from key to running total: O(n) time, O(g) space for g groups, plus O(g log g) to sort.
- Pulling the combine step out into a function, so
SUMandMAXshare one loop. - Edge cases: empty input, one group, negative values, a group whose total is 0.
Follow-ups
- Support
MAXas well asSUM, chosen by the caller. What changes forAVG, which can't be combined from two averages alone? - Group by several columns, or aggregate several columns in one pass.
- The table is too large for memory: stream it in chunks and merge partial sums, or partition by a hash of the key and aggregate each partition (as map-reduce does). Which aggregations still work with this approach?
Where it's asked
- Notion · Technical phone screen · Last asked Apr 202660 min · Live coding in CoderPad
What it tests
aggregation · sql · tables
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
