Latest Commit Build Cost

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
L5
Asked in
Tredence

Problem

Our heaviest CI spend sits on repositories built by outside webhooks, so every engineer who has committed to a webhook-built repository shares that bill. For each of those engineers, find the repository in repo_commits they most recently committed to and return its total build seconds across every build recorded in ci_builds.

The latest commit came with a build cost.

Tables

repo_commits

Columns: commit_id INTEGER, repo_name TEXT, author TEXT, message TEXT, added INTEGER, removed INTEGER, commit_at TEXT

repo_commits: 10 of 200 rows
commit_idrepo_nameauthormessageaddedremovedcommit_at
3061backend-apibobAdd unit tests1772026-02-02 01:09:00
3122infracharlieRefactor auth module34142026-03-03 02:18:00
3183ml-pipeline charlieUpdate deps51212026-04-04 03:27:00
3244mobile-appdana Improve perf68282026-05-05 04:36:00
3305data-platformfrank Add caching layer 85352026-06-06 05:45:00
3366frontendAliceFix typo in readme102422026-07-07 06:54:00
3427backend-apiBobNULL119492026-08-08 07:03:00
3488infraaliceWIP136562026-09-09 08:12:00
3549ml-pipelinebob153632026-01-10 09:21:00
3610mobile-appcharlieAdd search endpoint170702026-02-11 10:30:00

ci_builds

Columns: build_id INTEGER, repo_name TEXT, branch TEXT, status TEXT, dur_secs INTEGER, trigger TEXT, built_at TEXT

ci_builds: 10 of 200 rows
build_idrepo_namebranchstatusdur_secstriggerbuilt_at
5047backend-apidevelopfailed34pull_request2026-02-02 01:13:00
5094infrafeature/authcanceled53schedule2026-03-03 02:26:00
5141ml-pipelinefix/login-bugrunning72manual2026-04-04 03:39:00
5188mobile-apprelease/v2.0queued91tag2026-05-05 04:52:00
5235data-platformhotfix/paymentsuccess110webhook2026-06-06 05:05:00
5282frontendfeature /spacesfailedNULLpush2026-07-07 06:18:00
5329backend-apimaincanceled148pull_request2026-08-08 07:31:00
5376infradeveloprunning167schedule2026-09-09 08:44:00
5423ml-pipelinefeature/authqueued186manual2026-01-10 09:57:00
5470mobile-appfix/login-bugsuccess205tag2026-02-11 10:10:00

Expected output

First 4 of 4 rows
authortotal_build_seconds
Bob14680
bob14680
dana14680
frank15456

Worked solution and explanation

What this problem is really about

Under the billing story this is a membership-gated latest-touch rollup. The cost lives on the repository, not the person, so the work is choosing the RIGHT people and the RIGHT repo for each: only engineers who have committed to a webhook-built repository are on the hook, and each of them is billed the repository they most recently touched. The subtle part is that the webhook test decides WHO gets billed, not WHICH builds are counted.


Why the obvious query is wrong

The reflex is GROUP BY author with MAX(commit_at), then a re-join to repo_commits to recover repo_name. That loses the repo the max belongs to and fans out on ties. Two more traps hide here: skip the roster subquery and you bill every engineer, including those who never touched a webhook-built repo; and if you read the webhook clause as a filter on ci_builds and sum only trigger = 'webhook' builds, the chosen repo's real cost collapses.


Building it step by step

Step 1: Pin down who is billed

Build the roster with WHERE author IN (SELECT rc.author FROM repo_commits rc JOIN ci_builds cb ON rc.repo_name = cb.repo_name WHERE cb.trigger = 'webhook'). That keeps only engineers who committed to a repository that runs webhook builds. Drop it and every author flows through, inflating the bill.

Step 2: Pick each engineer's most recent repo

ROW_NUMBER() OVER (PARTITION BY author ORDER BY commit_at DESC, commit_id DESC), then keep rn = 1. The window carries repo_name on the winning row, which a GROUP BY author with MAX(commit_at) cannot, and commit_id DESC settles a same-timestamp tie deterministically.

Step 3: Roll up the chosen repo's cost

Join the winning (author, repo_name) back to ci_builds on repo_name, take SUM(dur_secs), and GROUP BY author. Every build for that repo counts, whatever its status.


The solution

LATEST COMMIT BUILD COST
WITH latest_repo AS (
  SELECT author, repo_name,
         ROW_NUMBER() OVER (PARTITION BY author ORDER BY commit_at DESC, commit_id DESC) AS rn
  FROM repo_commits
  WHERE author IN (
    SELECT rc.author
    FROM repo_commits rc
    JOIN ci_builds cb ON rc.repo_name = cb.repo_name
    WHERE cb.trigger = 'webhook'
  )
)
SELECT lr.author, SUM(cb.dur_secs) AS total_build_seconds
FROM latest_repo lr
JOIN ci_builds cb ON lr.repo_name = cb.repo_name
WHERE lr.rn = 1
GROUP BY lr.author;

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions