JavaScript Group By ID with Average Score: Two Approaches Quiz
Two seeded ways to group records by id and compute each group's average (bucket-then-divide vs running sum + count), plus two companions on min/max per group and on memory trade-offs at scale.
Question Bank
Hard
JavaScript
quiz
arrays
hash-map
array-manipulation-patterns
329 views
5
Implement groupedAverageScore(arr, key) so it groups items by the property named key, then returns an object mapping each group key to { total, average } where total is the group size and average is the mean of score. Round each average to four decimal places.
Examples
Example 1:
Input: groupedAverageScore([{id:1,score:10},{id:1,score:20},{id:2,score:30}], 'id')
Output: { '1': { total: 2, average: 15 }, '2': { total: 1, average: 30 } }
Explanation: group 1 has two scores 10 and 20 (average 15) and group 2 has one score 30 (average 30).3 more questions, with full solutions and explanations, are available for premium members.
Upgrade to Premium