HomeChallengesRepsProblemsLeaderboardTake a challenge

Group and Total

The dashboard needs revenue per category and the top two sellers. Four small functions, and every reporting screen you ever build is made of them.

Beginner15 min on the clock5 graded checks · 100 pointsData Structures at Work
Graded by running your code against real cases.

What you build

  • groupBy(items, keyFn) — a Map from key to the items with that key
  • countBy(items, keyFn) — a Map from key to how many
  • sumBy(items, keyFn, valueFn) — a Map from key to the total
  • topN(totals, n) — the n largest as [key, value] pairs, biggest first
  • One case groups 20,000 rows on a time budget

Done means

Every total is right, ties do not crash, and 20,000 rows group inside the budget.

How it is graded

Published in full, before you start — every point is one of these and there is nothing else. Each one runs your code; it is not a search for keywords.

  1. groupBy buckets the items+25Indexes and lookups
  2. countBy counts them+20Indexes and lookups
  3. sumBy totals them+25Indexes and lookups
  4. topN picks the biggest+20Indexes and lookups
  5. It groups 20,000 rows in time+10The cost of a loop inside a loop

What it teaches

Indexes and lookupsbeginner

An index is a Map built once so you can find a thing by its id instantly, instead of searching the whole list every time you need it — which is the difference between a table that renders instantly and one that takes four seconds.

The cost of a loop inside a loopintermediate

A search inside a loop does the work once per row per row: at 20,000 rows that is 400 million comparisons, and it is the single most common reason a working feature is unusably slow.

The rest of Data Structures at Work