Code Room
CodingMediumcod-g1156
Subject Database aggregationLevel Mid–Senior~25 minCommon in Databases & SQL interviewsIndustries Software development

Question

You are implementing the GROUP BY / HAVING stage of a query engine. Given a list of rows, each a dict with keys 'dept' (string) and 'salary' (int), group the rows by 'dept', compute the average salary per group, and return only the groups whose average salary is strictly greater than `threshold`. Return a list of [dept, avg_salary] pairs where avg_salary is the integer floor of the average. Order the result by dept ascending. There are at most 10000 rows; salaries are non-negative.

Implement
having_avg_salary(rows: list[dict], threshold: int) → list[list]
Examples
in[[{"dept":"eng","salary":100},{"dept":"eng","salary":200},{"dept":"sales","salary":50}],80]out[["eng",150]]
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.