Group by with aggregate
Implement GROUP BY with an aggregate. Given rows (list of dicts), a group_col name, an agg_col name, and an agg function one of 'sum','count','avg', group rows by row[group_col] and compute the aggregate of row[agg_col] within each group. 'count' ignores agg_col and counts rows; 'avg' is the floating-point mean. Return a list of [group_value, aggregate] pairs sorted ascending by group_value. avg values must equal the exact float mean.
Implement
group_aggregate(rows: list[dict], group_col: str, agg_col: str, agg: str) → list[list]Examples
in
[[{"sal":100,"dept":"eng"},{"sal":200,"dept":"eng"},{"sal":50,"dept":"hr"}],"dept","sal","sum"]out[["eng",300],["hr",50]]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 25 min
solution.py
InputExpectedGot
[[{"sal":100,"dept":"eng"},{"sal":200,"dept":"eng"},{"sal":50,"dept":"hr"}],"dept","sal","sum"][["eng",300],["hr",50]]not run yetsample