Live Wire

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
Netflix

Problem

Every change to a feature flag lands in feat_flags as a new row, so a single flag shows up many times across its history. For each owning team, find the flag they touched most recently and when that change landed, with the most recently active team first.

The last switch each team flipped.

Tables

feat_flags

Columns: flag_id INTEGER, flag_name TEXT, enabled INTEGER, rollout REAL, owner TEXT, created TEXT, updated TEXT

feat_flags: 10 of 200 rows
flag_idflag_nameenabledrolloutownercreatedupdated
131new_checkout0NULLengineering2025-02-022026-02-02
162beta_search0NULLgrowth2025-03-032026-03-03
193ai_recs139platform2025-04-042026-04-04
224live_chat0NULLmobile2025-05-052026-05-05
255video_upload0NULLdata2025-06-062026-06-06
286two_factor178product2025-07-072026-07-07
317bulk_export0NULLengineering2025-08-082026-08-08
348new_dashboard0NULLgrowth2025-09-092026-09-09
379mobile_v2117platform2025-10-102026-01-10
410dark_mode0NULLmobile2025-11-112026-02-11

Expected output

First 5 of 6 rows
ownerflag_namelast_changed
growthnew_dashboard2026-09-09
datavideo_upload2026-09-08
productdark_mode2026-09-07
platformvideo_upload2026-09-06
mobilebeta_search2026-08-25

Worked solution and explanation

What this is really testing

This is a most-recent-row-per-group problem wearing a feature-flag costume. Each edit appends a new row, so one flag shows up many times and feat_flags is a change log, not a snapshot. The skill being probed: can you pull each team's single latest row and carry its flag_name along, not just the newest timestamp? Anyone can write MAX(updated) per owner. The trap is pairing that max date with a bare flag_name in the same GROUP BY. That either fails to compile or hands you a name you cannot vouch for, so you report the right date next to a flag you cannot defend. Get it wrong and you tell the platform team its newest change was to a flag it actually touched months ago.


How to get there

Step 1: Number each team's rows by recency

Number the rows within each owner by recency: ROW_NUMBER() OVER (PARTITION BY owner ORDER BY updated DESC, flag_id DESC). The row with rn = 1 is that team's most recent change. Because you keep the whole row, flag_name and updated stay attached. The flag_id tie-break makes the pick deterministic if two changes ever share a date.

Step 2: Keep the latest row and order by recency

Keep only rn = 1, then project owner, flag_name and the timestamp as last_changed. Sort by last_changed descending so the team that touched a flag most recently sits at the top. No grouping is needed: the window has already isolated one row per team.


The solution

Latest row per team, name and date kept together
WITH ranked AS (
    SELECT
        owner,
        flag_name,
        updated,
        ROW_NUMBER() OVER (
            PARTITION BY owner
            ORDER BY updated DESC, flag_id DESC
        ) AS rn
    FROM feat_flags
)
SELECT
    owner,
    flag_name,
    updated AS last_changed
FROM ranked
WHERE rn = 1
ORDER BY last_changed DESC
Naive: aggregate one column
SELECT owner, flag_name, MAX(updated) FROM feat_flags GROUP BY owner. The MAX finds the newest timestamp, but flag_name is not bound to that row. Depending on the engine you get an error or a name you cannot justify.
Correct: isolate the whole row
ROW_NUMBER() partitions by owner and orders by recency, then you keep rn = 1. flag_name and updated travel together from the same physical row, so the pairing is always honest.

Common follow-up questions