The Switchboard

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
L3
Asked in
Credit Karma

Problem

A feature-flag service writes a new record every time a flag is switched on or off, so one flag can appear many times with different settings. For each flag, give its current state as two indicators: a 1/0 for whether it is enabled and a 1/0 for whether it is disabled, listed alphabetically by flag name.

On or off. Every flag at a glance.

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_nameenabled_flagdisabled_flag
ai_recs01
beta_search01
bulk_export01
dark_mode10
live_chat01

Worked solution and explanation

What this problem really is

Under the 'binary indicators' costume, this is a latest-record-per-flag problem. The feat_flags table is an append-only toggle log: every on/off switch writes a fresh row, so a flag you picture as one thing is really a stack of historical settings. Turning enabled into a 1/0 pair is the easy half that everyone gets. What separates candidates is noticing they must first collapse each flag to its single current record. Skip that step and you emit one row for every toggle a flag ever had, with stale on/off values mixed into the result.


Reading the grain

Step 1: Recognize the toggle log

flag_name is not unique in feat_flags. The same flag shows up once per toggle, each with its own updated timestamp and enabled value. 'For each flag' therefore means one row per flag_name reflecting its most recent setting, not one row per raw event.

Step 2: Collapse to the current record

Number the rows within each flag using ROW_NUMBER() OVER (PARTITION BY flag_name ORDER BY updated DESC, flag_id DESC) and keep rn = 1. Ordering by updated DESC picks the newest toggle; flag_id DESC is a deterministic tiebreaker so two switches on the same day still resolve to one stable winner.

Step 3: Emit the binary pair

enabled is stored as 0 or 1, so enabled_flag is simply enabled and disabled_flag is its complement, 1 - enabled. Order the final result by flag_name so downstream consumers get a stable, predictable ordering.


The solution

Current on/off state per flag
WITH latest AS (
    SELECT
        flag_name,
        enabled,
        ROW_NUMBER() OVER (
            PARTITION BY flag_name
            ORDER BY updated DESC, flag_id DESC
        ) AS rn
    FROM feat_flags
)
SELECT
    flag_name,
    enabled AS enabled_flag,
    1 - enabled AS disabled_flag
FROM latest
WHERE rn = 1
;
Naive: read enabled directly
SELECT flag_name, enabled AS enabled_flag, 1 - enabled AS disabled_flag FROM feat_flags ORDER BY flag_name; returns every toggle ever recorded, duplicating flags and leaking stale states.
Correct: dedup then transform
Rank rows per flag by recency, keep rn = 1, then compute the binary pair. Exactly one current row per flag, no stale values.

Common follow-up questions