Deploy Velocity Swings

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
Walmart

Problem

Each row of deploy_logs is one deployment of a service, whatever its outcome, timed by deploy_at. List every service's months with that month's deployment count, the count from its most recent earlier month with deployments, and the percentage change between them rounded to 2 decimals, services alphabetically and months oldest first. A service's first month has nothing to compare against, so it is left out.

Month to month, who sped up and who stalled.

Tables

deploy_logs

Columns: log_id INTEGER, svc_name TEXT, version TEXT, env_name TEXT, status TEXT, deploy_at TEXT, dur_secs INTEGER, author TEXT

deploy_logs: 10 of 200 rows
log_idsvc_nameversionenv_namestatusdeploy_atdur_secsauthor
131payment-apiv1.0.2stagingfailed2026-02-02 01:11:0023bob
162user-svcv1.1.0devrolled_back2026-03-03 02:22:0036charlie
193search-api2.0.0canaryin_progress2026-04-04 03:33:0049dana
224gatewayv2.1.0ProductionSuccess2026-05-05 04:44:0062eve
255notif-svc2.1.1STAGINGFAILED2026-06-06 05:55:0075frank
286analyticsv3.0.0-rc1productionsuccess2026-07-07 06:06:0088Alice
317ml-servingv1.0.3stagingfailed2026-08-08 07:17:00NULLBOB
348auth-svcv1.0.1devRolled_Back2026-09-09 08:28:00114alice
379Payment-APIv1.0.2canaryin_progress2026-01-10 09:39:00127bob
410user-svcv1.1.0ProductionSuccess2026-02-11 10:50:00140charlie

Expected output

First 5 of 144 rows
svc_namedeploy_monthdeploy_countprev_countpct_change
analytics2022-06110
analytics2022-10110
analytics2026-0321100
analytics2026-0521100
analytics2026-073250

Worked solution and explanation

Why this problem exists in real interviews

This is percentage-change velocity dressed up as deployment metrics. The real skill: computing relative change against the prior observation while dropping the first one per service, which has nothing to compare against. Anyone can reach for the window function; the trap is the baseline (each service's first month must be excluded, not zeroed) and the divide-by-zero that lurks when a service had no deploys the previous month. Get the baseline wrong and every service sprouts a bogus first row; miss the zero denominator and the query errors out on real data.


Break down the requirements

Step 1: Count deploys per service per month

Aggregate deploy_logs to deployment count per service per month.

Step 2: Compute previous month count with LAG

Use LAG over (partition by service, order by month) to get the previous month's count.

Step 3: Calculate percentage change

Compute the percentage change from previous to current, excluding rows with no previous month.


The solution

Partitioned percentage change with LAG
WITH monthly AS (SELECT svc_name, strftime('%Y-%m', deploy_at) AS deploy_month, COUNT(*) AS deploy_count FROM deploy_logs GROUP BY svc_name, deploy_month), with_lag AS (SELECT svc_name, deploy_month, deploy_count, LAG(deploy_count) OVER (PARTITION BY svc_name ORDER BY deploy_month) AS prev_count FROM monthly) SELECT svc_name, deploy_month, deploy_count, prev_count, ROUND((deploy_count - prev_count)*100.0/prev_count,2) AS pct_change FROM with_lag WHERE prev_count IS NOT NULL ORDER BY svc_name, deploy_month

Common follow-up questions