Crossed Signals

A hard SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
hard
Seniority
L5
Asked in
Etsy

Problem

Report, for each ad campaign, how many alerts it drew and how many of those alerts have a status of resolved. The ops team credits an alert to a campaign when one of that campaign's ad_impressions landed in the same calendar hour, on the same date, as the alert fired in alert_events, because the ad platform and the alerting system share no key, only a wall clock.

No shared key, only a shared clock.

Tables

alert_events

Columns: alert_id INTEGER, svc_name TEXT, severity TEXT, status TEXT, fired_at TEXT, ack_by TEXT, resolved TEXT

alert_events: 10 of 200 rows
alert_idsvc_nameseveritystatusfired_atack_byresolved
129payment-apihighacknowledged2026-02-02 01:07:00bob2026-02-02 03:11:00
158user-svcmediumresolved2026-03-03 02:14:00charlie2026-03-03 04:22:00
187search-apilowsilenced2026-04-04 03:21:00danaNULL
216gatewayCriticalfiring2026-05-05 04:28:00NULL2026-05-05 06:44:00
245notif-svcHIGHacknowledged2026-06-06 05:35:00alice2026-06-06 07:55:00
274db-primarycriticalresolved2026-07-07 06:42:00bobNULL
303cache-01highsilenced2026-08-08 07:49:00charlie2026-08-08 09:17:00
332auth-svcmediumfiring2026-09-09 08:56:00NULL2026-09-09 10:28:00
361payment-apilowacknowledged2026-01-10 09:03:00eveNULL
390user-svcCriticalresolved2026-02-11 10:10:00alice2026-02-11 12:50:00

ad_impressions

Columns: impression_id INTEGER, user_id INTEGER, ad_campaign TEXT, impression_time TEXT, clicked INTEGER, revenue REAL

ad_impressions: 10 of 200 rows
impression_iduser_idad_campaignimpression_timeclickedrevenue
253100BRAND_AWARENESS_Q12026-02-02 01:07:000NULL
306197PRODUCT_LAUNCH_X2026-03-03 02:14:000NULL
359294RETARGETING_CART2026-04-04 03:21:000NULL
412391HOLIDAY_PROMO2026-05-05 04:28:000NULL
465488NEW_USER_ACQU2026-06-06 05:35:0010.2
518NULLLOYALTY_PROGRAM2026-07-07 06:42:000NULL
571682FLASH_SALE_48H2026-08-08 07:49:000NULL
624779SUMMER_SALE_20242026-09-09 08:56:000NULL
677876BRAND_AWARENESS_Q12026-01-10 09:03:000NULL
730973PRODUCT_LAUNCH_X2026-02-11 10:10:0010.35

Expected output

First 5 of 8 rows
ad_campaignalert_countresolved_count
BRAND_AWARENESS_Q1170
FLASH_SALE_48H150
HOLIDAY_PROMO160
LOYALTY_PROGRAM1515
PRODUCT_LAUNCH_X1616

Worked solution and explanation

What this problem is really testing

Strip the costume and this is a many-to-many join between two tables that were never meant to touch. ad_impressions and alert_events share no foreign key. The only link is timing: an alert and an impression that happened in the same hour. The real skill being probed is whether you notice that the hour bucket fans out. One alert hour matches every impression in that hour, so a plain COUNT(alert_id) tallies each alert once per impression. Your alert counts quietly become impression counts, and the campaign with the most ad traffic floats to the top, which is not what the question asks.


Walk the build

Step 1: Name the join key

There is no foreign key between the tables. Bucket both timestamps to the hour with SUBSTR(impression_time, 1, 13) and SUBSTR(fired_at, 1, 13), then match on that string. Keep it an inner join, so a campaign whose impressions never line up with an alert hour drops out. Say this out loud before writing SQL, so the interviewer knows you saw the missing key rather than assuming one.

Step 2: Dedupe the fan-out

Because the bucket join is many-to-many, count with COUNT(DISTINCT alert_id). Without the distinct, every alert is multiplied by the number of impressions sharing its hour, and the metric stops meaning what its name says.

Step 3: Count resolved conditionally

COUNT(DISTINCT CASE WHEN status = 'resolved' THEN alert_id END) keeps the dedup around the conditional, so a resolved alert that matched several impressions still counts once. Read resolved from status, not from the resolved timestamp column; the two disagree on real rows.

Step 4: Group, and order only for people

GROUP BY ad_campaign gives one row per campaign. Grading ignores row order, so the answer needs no ORDER BY. If a person will read the output, sort by alert_count descending for them, but that is presentation, not part of the answer.


The solution

Crossed Signals
WITH matched AS (
  SELECT ai.ad_campaign,
         ae.alert_id,
         ae.status
  FROM ad_impressions ai
  INNER JOIN alert_events ae
    ON SUBSTR(ai.impression_time, 1, 13) = SUBSTR(ae.fired_at, 1, 13)
)
SELECT ad_campaign,
       COUNT(DISTINCT alert_id) AS alert_count,
       COUNT(DISTINCT CASE WHEN status = 'resolved' THEN alert_id END) AS resolved_count
FROM matched
GROUP BY ad_campaign

Common follow-up questions

Common follow-up questions