Tenure Mentorship Match
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
- Stitch Fix
Problem
Nobody's start date is on record, so whoever opened the earliest review on a repo counts as its longest-standing author. An engineering manager wants every review in code_reviews paired with that author as its mentor, listed repo by repo alphabetically, then by author name, then by review id.
Pair by tenure. Longest with newest.
Tables
code_reviews
Columns: review_id INTEGER, repo_name TEXT, author TEXT, reviewer TEXT, status TEXT, comments INTEGER, opened_at TEXT, merged TEXT
| review_id | repo_name | author | reviewer | status | comments | opened_at | merged |
|---|---|---|---|---|---|---|---|
| 1053 | backend-api | bob | eve | changes_requested | 3 | 2026-02-02 | 2026-02-05 |
| 1106 | infra | charlie | frank | pending | 6 | 2026-03-03 | 2026-03-06 |
| 1159 | ml-pipeline | dana | grace | Approved | 9 | 2026-04-04 | 2026-04-07 |
| 1212 | mobile-app | eve | hank | PENDING | 12 | 2026-05-05 | NULL |
| 1265 | data-platform | frank | alice | merged | 15 | 2026-06-06 | 2026-06-09 |
| 1318 | frontend | grace | bob | approved | 18 | 2026-07-07 | 2026-07-10 |
| 1371 | backend-api | hank | charlie | changes_requested | 21 | 2026-08-08 | 2026-08-11 |
| 1424 | infra | alice | NULL | pending | 24 | 2026-09-09 | NULL |
| 1477 | ml-pipeline | bob | eve | Approved | 27 | 2026-01-10 | 2026-01-13 |
| 1530 | mobile-app | charlie | frank | PENDING | 30 | 2026-02-11 | 2026-02-14 |
Expected output
| review_id | repo_name | author | most_tenured_author |
|---|---|---|---|
| 1053 | backend-api | bob | bob |
| 2325 | backend-api | bob | bob |
| 1901 | data-platform | bob | bob |
| 2272 | frontend | alice | grace |
| 1424 | infra | alice | charlie |
Worked solution and explanation
What this really is
This is a per-group argmin broadcast dressed up as a mentorship program. Anyone can find each repo's earliest opened_at with MIN. What separates candidates is knowing that `MIN` hands you a timestamp, not the person who owns it, and that the answer needs that person stamped onto every review in the repo. Put author next to MIN(opened_at) and you get an error or an arbitrary author; stop at one row per repo and every review you were meant to route is gone.
Building it
Step 1: Order each repo's reviews oldest first
Number the rows of code_reviews inside each repo_name by opened_at, with review_id as the second key so a same-second tie still has one winner. Row 1 of each repo is the review that proves who has been around longest, and it carries its own author with it, which is exactly what MIN cannot do.
Step 2: Keep only rank 1
Filtering tenure_rank = 1 leaves one row per repo: the repo and its most tenured author. This is the argmin, and it is tiny: one row per repo, however many reviews there are.
Step 3: Broadcast the winner back onto every review
Join code_reviews to that small set on repo_name. Every review picks up its repo's mentor and keeps its own row, so a repo with forty reviews still returns forty rows. Putting tenure_rank = 1 in the join condition is what stops the fan-out.
Step 4: Sort the roster
repo_name, then author, then review_id, all ascending and all returned, so the manager reads each repo as one block and each author's reviews in id order.
WITH repo_first_author AS (
SELECT
repo_name,
author AS most_tenured_author,
ROW_NUMBER() OVER (PARTITION BY repo_name ORDER BY opened_at, review_id) AS tenure_rank
FROM code_reviews
)
SELECT
r.review_id,
r.repo_name,
r.author,
f.most_tenured_author
FROM code_reviews r
JOIN repo_first_author f
ON f.repo_name = r.repo_name
AND f.tenure_rank = 1
ORDER BY r.repo_name, r.author, r.review_id- Minimum plus a tagalong author
SELECT repo_name, MIN(opened_at), author FROM code_reviews GROUP BY repo_nameeither fails becauseauthoris not grouped, or, where an engine allows it, returns some author from the group with no link to the minimum. It also returns one row per repo, not one per review.- Argmin, then join back
- Order inside the repo, keep rank 1, and join it back. The
authortravels with the row that won, and the join restores one row per review.
Common follow-up questions
- How would you stop the mentor's own reviews from pairing with themselves and give them the runner-up instead? (Tests keeping rank 2 and choosing between the two per review.)
- What changes if tenure is measured across all repos rather than within each one? (Drops the partition key; the argmin becomes a single company-wide winner.)
- Could you also show how many reviews the mentor has opened on that repo? (Layers a second per-repo, per-author aggregate onto the CTE without breaking the grain.)
- What if `code_reviews` grows to a billion rows and is refreshed daily? (Probes maintaining the per-repo earliest review incrementally instead of re-sorting history.)