# Deploy Velocity

> Days between deploys. Some services ship fast, others crawl.

Canonical URL: <https://datadriven.io/problems/deploy_velocity>

Domain: SQL · Difficulty: medium · Seniority: L5

## Problem

The release manager is benchmarking how often each service ships, but a deploy only counts if it logged a run duration: rows with no recorded duration were never instrumented and don't reflect real cadence. For each service, average the gaps in days between its consecutive counted deploys, listed alphabetically by name.

## Tables

### deploy_logs

- log_id INTEGER
- svc_name TEXT
- version TEXT
- env_name TEXT
- status TEXT
- deploy_at TEXT
- dur_secs INTEGER
- author TEXT

Sample rows (10 of 200):

| log_id | svc_name | version | env_name | status | deploy_at | dur_secs | author |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 131 | payment-api | v1.0.2 | staging | failed | 2026-02-02 01:11:00 | 23 | bob |
| 162 | user-svc | v1.1.0 | dev | rolled_back | 2026-03-03 02:22:00 | 36 | charlie |
| 193 | search-api | 2.0.0 | canary | in_progress | 2026-04-04 03:33:00 | 49 | dana |
| 224 | gateway | v2.1.0 | Production | Success | 2026-05-05 04:44:00 | 62 | eve |
| 255 | notif-svc | 2.1.1 | STAGING | FAILED | 2026-06-06 05:55:00 | 75 | frank |
| 286 | analytics | v3.0.0-rc1 | production | success | 2026-07-07 06:06:00 | 88 | Alice |
| 317 | ml-serving | v1.0.3 | staging | failed | 2026-08-08 07:17:00 | NULL | BOB |
| 348 | auth-svc | v1.0.1 | dev | Rolled_Back | 2026-09-09 08:28:00 | 114 | alice |
| 379 | Payment-API | v1.0.2 | canary | in_progress | 2026-01-10 09:39:00 | 127 | bob |
| 410 | user-svc | v1.1.0 | Production | Success | 2026-02-11 10:50:00 | 140 | charlie |

## Expected output

First 5 of 11 rows:

| svc_name | avg_gap_days |
| --- | --- |
|  payment-api | NULL |
| Payment-API | NULL |
| analytics | 88.26228070175766 |
| auth-svc | 76.45939153439498 |
| gateway | 76.12526455027096 |

## Worked solution and explanation

### What this problem is really about

Strip the costume and this is a per-service inter-event gap analysis with a data-quality gate on the front. The skill: reach each service's previous deploy with `LAG`, but only across the deploys that actually count. The trap is WHERE the filter goes. The uninstrumented rows, the ones with no `dur_secs`, have to leave the set BEFORE the window runs, so every gap spans two real releases. Filter after the window, or not at all, and `LAG` measures from a deploy that should not exist, and each service's cadence number comes out wrong.

---

### Walk the requirements

#### Step 1: Drop the deploys that never counted

Keep only rows where `dur_secs` is present. A deploy with no recorded duration was never instrumented, so it must leave the set before any gap is measured. Do this in the CTE's `WHERE` so the window only ever sees counted deploys.

#### Step 2: Reach the previous qualifying deploy

Within each service, order by `deploy_at` and use `LAG(deploy_at) OVER (PARTITION BY svc_name ORDER BY deploy_at)` to pull the prior qualifying deploy's timestamp onto the current row.

#### Step 3: Turn each pair into days, then average

`julianday(deploy_at) - julianday(prev)` gives the gap in fractional days. A service's first deploy has no predecessor, so its gap is `NULL`; `AVG` skips those on its own, so grouping by `svc_name` and averaging leaves each service's true cadence.

---

### The solution

**LAG-based deployment cadence with a duration gate**

```sql
WITH gaps AS (
    SELECT
        svc_name,
        julianday(deploy_at) - julianday(LAG(deploy_at) OVER (PARTITION BY svc_name ORDER BY deploy_at)) AS gap_days
    FROM deploy_logs
    WHERE dur_secs IS NOT NULL
)
SELECT
    svc_name,
    AVG(gap_days) AS avg_gap_days
FROM gaps
GROUP BY svc_name
ORDER BY svc_name
```

> **Why the plan stays cheap**
>
> A window function over the rows, partitioned by `svc_name` and sorted by `deploy_at`. The `dur_secs` filter runs first and shrinks what the window has to sort; the final `GROUP BY` collapses each service to a single row.

> **Filter before the window, not after**
>
> The tell is WHERE the `dur_secs` filter sits. A strong candidate puts it inside the CTE so `LAG` only ever sees counted deploys; a weaker one filters on `gap_days` afterward and silently measures gaps across rows that should not be there.

> **Timestamp gaps are not text gaps**
>
> The gap type follows the column type. Here `deploy_at` is text, so `julianday` is the clean path; on a real `TIMESTAMP` column the difference is an interval and you would pull days with something like EXTRACT(EPOCH FROM gap) / 86400.

---

## Common follow-up questions

- How would you flag services whose deploy frequency is accelerating? _(Compare recent gaps to older gaps, or fit a trend per service.)_
- What happens to a service with only one qualifying deploy? _(Every gap is NULL, so AVG returns NULL and the service carries no cadence.)_
- How would you also surface the tightest and widest gap per service? _(Add MIN(`gap_days`) and MAX(`gap_days`) alongside the AVG in the outer SELECT.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/deploy_velocity)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.