Many Eyes

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
BuzzFeed

Problem

Each row of code_reviews is one review of a repo, stamped with who reviewed it and the date it was opened. For every repo in each calendar year its reviews were opened, find how many different people reviewed its code, reporting the year as a whole number.

Every codebase draws its own circle of watchers. Count who shows up.

Tables

code_reviews

Columns: review_id INTEGER, repo_name TEXT, author TEXT, reviewer TEXT, status TEXT, comments INTEGER, opened_at TEXT, merged TEXT

code_reviews: 10 of 200 rows
review_idrepo_nameauthorreviewerstatuscommentsopened_atmerged
1053backend-apibobevechanges_requested32026-02-022026-02-05
1106infracharliefrankpending62026-03-032026-03-06
1159ml-pipelinedanagraceApproved92026-04-042026-04-07
1212mobile-appevehankPENDING122026-05-05NULL
1265data-platformfrankalicemerged152026-06-062026-06-09
1318frontendgracebobapproved182026-07-072026-07-10
1371backend-apihankcharliechanges_requested212026-08-082026-08-11
1424infraaliceNULLpending242026-09-09NULL
1477ml-pipelinebobeveApproved272026-01-102026-01-13
1530mobile-appcharliefrankPENDING302026-02-112026-02-14

Expected output

First 5 of 30 rows
repo_namereview_yearreviewer_count
backend-api20224
backend-api20234
backend-api20243
backend-api20253
backend-api20264

Worked solution and explanation

What this really is

Strip off the code-review costume and this is a two-dimensional distinct count: for every (repo, year) pair, how many unique people reviewed the code. Anyone can type GROUP BY. The line that separates candidates is COUNT(DISTINCT reviewer) versus a plain COUNT: one reviewer who left five comments on the same repo in the same year is one person, not five. Miss the DISTINCT and you are reporting review VOLUME while your column header claims reviewer HEADCOUNT, inflating collaboration density by whatever the comment rate happens to be. The second quiet trap is the grain: the year has to live in the GROUP BY, not just the SELECT, or every year collapses into one bucket per repo.


Getting the grain right

Step 1: Make the year part of the key

The reporting unit is a repo in a specific year, so the year is part of the key, not decoration. Pull it out of opened_at with strftime('%Y', opened_at) and put that expression in BOTH the SELECT and the GROUP BY. If you only add it to SELECT, the engine still buckets by repo alone and you get one arbitrary year stamped on a repo's whole history.

Step 2: Count people, not rows

Reviewer is the thing we are counting, and people repeat. COUNT(DISTINCT reviewer) is the only form that answers 'how many different people', and it quietly does the right thing with unassigned reviews too: NULL reviewers are skipped, so a review with no one assigned does not pad the number.

Step 3: Make the year a real number

strftime('%Y', opened_at) returns TEXT ('2026'), and the answer wants a whole-number year, so wrap it in CAST(... AS INTEGER). The prompt asks for the year as a number, and this is where you honor it: review_year becomes a real integer instead of a string that only looks numeric.


The solution

Distinct reviewers per repo per year
SELECT repo_name, CAST(strftime('%Y', opened_at) AS INTEGER) AS review_year, COUNT(DISTINCT reviewer) AS reviewer_count
FROM code_reviews
GROUP BY repo_name, review_year

Common follow-up questions