Group By and Aggregate
Implement the core of a GROUP BY for an analytics endpoint. Given records (plain objects), group them by the value of groupKey (converted with String) and aggregate the numeric field valueKey.
Records whose valueKey is not a number (missing, null, a string, and so on) are skipped entirely; a group exists only if at least one record in it is kept.
Return one object per group, { group, count, sum, avg, min, max }, sorted by group ascending in plain string order. avg is sum / count rounded to 2 decimals with Math.round(x * 100) / 100.
Example 1
Input: records = [{"region":"north","sales":10},{"region":"south","sales":7},{"region":"north","sales":30},{"region":"south","sales":"5"}], groupKey = "region", valueKey = "sales" Output: [{"group":"north","count":2,"sum":40,"avg":20,"min":10,"max":30},{"group":"south","count":1,"sum":7,"avg":7,"min":7,"max":7}] Explanation: north: 10 and 30; south has one kept record (the string "5" is skipped).
Example 2
Input: records = [], groupKey = "k", valueKey = "v" Output: []
Constraints
- 0 <= records.length <= 10^5
- Every kept record has groupKey present
- Values are integers between -10^6 and 10^6
Hint 1
A Map from group to running totals keeps this to one pass.
Hint 2
Use typeof v === "number" to decide whether to keep a record.
Good practice for: Backend Developer, Full Stack Developer, Data Engineer, Database Administrator, Data Scientist, Machine Learning Engineer
Run checks your code on 3 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.