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, a handoff to another team included, lands in feat_flags as a new row, so a single flag shows up many times across its history. Show each flag as it stands now: the team that owns it, whether it is switched on, and when it last changed, most recently changed flag 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 10 rows
flag_nameownerenabledlast_changed
new_dashboardgrowth02026-09-09
video_uploaddata02026-09-08
beta_searchgrowth02026-09-07
dark_modeproduct12026-09-07
bulk_exportdata02026-09-06

Worked solution and explanation

What this is really testing

This is a latest-version-per-record problem dressed up as a feature-flag report. feat_flags is an append-only change log: flag_id names one change, flag_name names the flag, and a handoff or a toggle is just another row. The skill being probed: can you collapse each flag's history to the one row that is current, and read everything about the flag off that row? Anyone can find MAX(updated) per flag. The trap is attaching owner and enabled to that max. Group by them too and you get one row per owner the flag ever had; leave them bare and you get values from a row you never chose. Either way you report that 'new_dashboard' still belongs to a team that handed it off long ago.


How to get there

Step 1: Decide what identifies a record

flag_id is unique on every row, so partitioning by it puts each change in its own group and keeps all 200 rows. The flag is flag_name, and that is the partition key.

Step 2: Number each flag's versions newest first

ROW_NUMBER() OVER (PARTITION BY flag_name ORDER BY updated DESC, flag_id DESC). updated is ISO text, so a descending string sort puts the newest first. The flag_id key settles two changes to one flag on the same day, so the pick stays deterministic when dates collide.

Step 3: Keep version one and read the flag off it

Filter version_rank = 1, project flag_name, owner, enabled and updated as last_changed, and sort by last_changed descending. Every column comes from the same physical row, so owner and toggle are the ones that held at the last change.


The solution

Current version of each flag
WITH versions AS (
    SELECT
        flag_name,
        owner,
        enabled,
        updated,
        ROW_NUMBER() OVER (
            PARTITION BY flag_name
            ORDER BY updated DESC, flag_id DESC
        ) AS version_rank
    FROM feat_flags
)
SELECT
    flag_name,
    owner,
    enabled,
    updated AS last_changed
FROM versions
WHERE version_rank = 1
ORDER BY last_changed DESC
The tempting version that returns history
SELECT
    flag_name,
    owner,
    enabled,
    MAX(updated) AS last_changed
FROM feat_flags
GROUP BY flag_name, owner, enabled

Grouping by owner and enabled splits each flag into one row per past combination.

Naive: aggregate the date
GROUP BY flag_name, owner, enabled with MAX(updated). Each flag returns once per owner it ever had, each row with that combination's newest date.
Correct: isolate the current row
ROW_NUMBER() partitioned by flag_name and ordered newest first, then keep version_rank = 1. One row per flag, every column from the latest change.

Common follow-up questions