The Fault Lines

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
DoorDash

Problem

To see which account groups have the roughest time in the product, find each account status's share of events that ended badly, given as a decimal fraction of all of that group's events. Treat an event as bad when its type is error, timeout, or crash.

Some account groups hit far more errors than others. Surface the ones that break most.

Tables

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

event_data

Columns: event_id INTEGER, event_type TEXT, user_id INTEGER, event_timestamp TEXT, payload TEXT, tags TEXT, properties TEXT

event_data: 10 of 200 rows
event_idevent_typeuser_idevent_timestamppayloadtagsproperties
3059button_click1002026-02-02 01:07:00{"button_id": "cancel", "element_class": "btn-primary", "position": {"x": 13, "y": 7}}["desktop", "web", "returning"]{"os": "Edge", "language": "US", "screen_size": "en", "utm_source": "mobile"}
3118purchase1972026-03-03 02:14:00{"amount": 56.93, "currency": "GBP", "items": [{"sku": "SKU1002", "qty": 3}]}["ios", "premium", "new_user", "organic"]{"country": "Windows", "device_type": "FR", "referrer": "fr", "utm_medium": "1920x1080", "browser": "twitter"}
3177signup2942026-04-04 03:21:00{"signup_method": "apple", "referral_code": "REF3X", "accepted_terms": true}["android", "free", "campaign_a", "referral", "experiment_v1"]{"language": "iOS", "screen_size": "es", "utm_source": "desktop", "session_id": "bing", "os": "organic"}
3236login3912026-05-05 04:28:00{"login_method": "password", "remember_me": true, "mfa_used": false}["web", "returning"]{"device_type": "JP", "referrer": "ja", "utm_medium": "1440x900"}
3295logout4882026-06-06 05:35:00{"action": "logout", "value": 50}["premium", "new_user", "organic"]{"screen_size": "de", "utm_source": "tablet", "session_id": "amazon", "os": "Chrome"}
3354search5852026-07-07 06:42:00{"query": "phone", "results_count": 102, "filters": {"category": "electronics"}}["free", "campaign_a", "referral", "experiment_v1"]{"referrer": "mobile", "utm_medium": "375x812", "browser": "email", "country": "Edge", "device_type": "US"}
3413add_to_cartNULL2026-08-08 07:49:00{"product_id": 1007, "quantity": 2, "price": 58.49, "variant": "medium"}["returning", "campaign_b", "featured", "experiment_v2", "ios"]{"utm_source": "fr", "session_id": "twitter", "os": "Safari", "language": "Windows", "screen_size": "FR"}
3472open7792026-09-09 08:56:00{"action": "open", "value": 80}["new_user", "organic"]{"utm_medium": "bing", "browser": "organic", "country": "iOS"}
3531crash8762026-01-10 09:03:00{"action": "crash", "value": 90}["campaign_a", "referral", "experiment_v1"]{"session_id": "direct", "os": "Firefox", "language": "macOS", "screen_size": "JP"}
3590error9732026-02-11 10:10:00{"action": "error", "value": 100}["campaign_b", "featured", "experiment_v2", "ios"]{"browser": "Chrome", "country": "Android", "device_type": "DE", "referrer": "de", "utm_medium": "tablet"}

Expected output

First 4 of 4 rows
account_statusnegative_rate
active0.13333333333333333
inactive0.14893617021276595
pending_verification0.14285714285714285
suspended0.17073170731707318

Worked solution and explanation

What this really is

This is a per-group conditional rate wearing a product-analytics costume. The real question: can you compute count(bad) over count(all) inside each account_status bucket without letting integer division flatten every rate to zero? Anyone can write the CASE expression. The two things that separate candidates are casting the numerator to REAL before the divide, and knowing this rate is event-weighted, so a single noisy account can swing a whole bucket. Get the cast wrong and every dashboard cell reads 0.


The traps

Step 1: Event-weighted, not user-weighted

The rate is bad_events / all_events within each status, so one power user with 1000 events dominates that bucket. Picture two suspended accounts: A fires 999 clean events and 1 error, B fires a single error. The event-weighted rate is 2 out of 1001, roughly 0.002, but the user-weighted rate is 0.5, since half the accounts hit a bad event. The prompt asks for the share of events, so event-weighted is right here. A senior candidate still names the fork aloud, because product teams often mean the user-weighted version.

Step 2: Integer division destroys the answer

CAST(SUM(...) AS REAL) / COUNT(*) forces floating-point division. Without the cast, SQLite (and Postgres with two integer operands, and MySQL in strict mode) does integer division. SUM = 1, COUNT = 3, result = 0. Every rate below 1.0 collapses to zero and every rate at or above 1.0 collapses to 1. The query runs cleanly, the column type looks plausible, and the metric is silently wrong until someone checks a bucket by hand.

Step 3: The join sets the grain and quietly drops rows

Grouping by u.account_status means every counted event must belong to a user whose status is known, and that happens through the INNER JOIN. Two silent drops: events with a NULL user_id (system events) never match a users row, and users who fired zero events never reach the SELECT at all. Both are usually fine for an event-weighted rate, but say it aloud: the denominator is events that joined, not all events and not all accounts.


The solution

Conditional rate per account status
SELECT
    u.account_status,
    CAST(SUM(CASE WHEN ed.event_type IN ('error', 'timeout', 'crash') THEN 1 ELSE 0 END) AS REAL)
        / COUNT(*) AS negative_rate
FROM users u
JOIN event_data ed ON u.user_id = ed.user_id
GROUP BY u.account_status

Common follow-up questions