Code Room
CodingMediumcod-g1078
Subject Query aggregationLevel Mid–Senior~25 minCommon in Databases & SQL interviewsIndustries Software development, Technology

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.