Group sum aggregation
Given a list of records (each a dict), a `group_key`, and a numeric `value_key`, compute the SUM of `value_key` for each distinct value of `group_key` — the equivalent of `SELECT group_key, SUM(value_key) GROUP BY group_key`. Return a list of `[group_value, total]` pairs sorted alphabetically by group value. If a total is integral, return it as an int rather than a float.
Implement
group_totals(rows: list[dict], group_key: str, value_key: str) → list[list]Examples
in
[[{"sales":10,"region":"west"},{"sales":5,"region":"east"},{"sales":3,"region":"west"}],"region","sales"]out[["east",5],["west",13]]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 20 min
solution.py
InputExpectedGot
[[{"sales":10,"region":"west"},{"sales":5,"region":"east"},{"sales":3,"region":"west"}],"region","sales"][["east",5],["west",13]]not run yetsample