KatInterview

Rank Advertisers by User Complaint Rate Over a 7-Day Window

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionsaggregation

Last updated

Write SQL that finds the worst advertisers by report-to-visit rate, picks each one's worst ad, and builds a daily leaderboard without double-counting visits or reports.

You work on an ad-safety team. Users open ad landing pages (visits) and sometimes flag an ad (reports). Use ANSI SQL. Treat today as 2025-09-01, and define "the last 7 days" as 2025-08-26 00:00:00 through 2025-09-01 23:59:59 UTC.

Schema

  • advertisers(advertiser_id INT, name TEXT)
  • ads(ad_id INT, advertiser_id INT, created_at TIMESTAMP)
  • ad_page_visits(visit_id BIGINT, ad_id INT, user_id BIGINT, visited_at TIMESTAMP)
  • ad_reports(report_id BIGINT, ad_id INT, user_id BIGINT, reported_at TIMESTAMP, reason TEXT)

Sample rows

ads

ad_idadvertiser_idcreated_at
11012025-08-15 10:00:00
21012025-08-20 11:00:00
31022025-08-25 12:00:00
41032025-08-28 09:00:00

ad_page_visits (10 rows): ad 1 has visits 1-3 (users 1001, 1002, 1003, on 08-26 and 08-27). Ad 2 has visits 4-5 (users 1001, 1004). Ad 3 has visits 6-7 (users 1002, 1005). Ad 4 has visits 8-10 (users 1006, 1002, 1007, from 08-31 to 09-01).

ad_reports (5 rows): report 500 (ad 1, user 1002), 501 (ad 1, user 1003), 502 (ad 2, user 1004), 503 (ad 3, user 1005), 504 (ad 4, user 1006). Each is filed a few minutes after the matching visit.

advertisers holds 101 Alpha Co, 102 Beta LLC and 103 Gamma Inc.

Tasks

A. Worst advertisers. An advertiser qualifies if, in the window, it has at least 1,000 page visits and at least 50 distinct reporting users. Rank qualifiers by report_rate = total_reports / total_visits descending. Write one query returning advertiser_id, total_visits, total_reports, unique_reporters, report_rate, rank, keeping only ranks 1 to 5. Use a window function (RANK or DENSE_RANK). Break ties by total_reports descending, then advertiser_id ascending.

B. Worst ad. Extend the query so that, for advertisers with more than one ad, it also returns the ad_id with the highest ad-level report rate in the window. Only ads with at least 100 visits are eligible. Break ties by ad-level report count descending, then ad_id ascending.

C. Daily leaderboard. Return one row per advertiser per day in the window, with that day's rank by report rate. Only include advertisers with at least 200 visits that day. Advertisers with zero reports that day must still appear with rate 0, and days with no visits must not appear.

D. Explain in SQL comments (1) how your query avoids double-counting when a user visits and reports within the same minute, and (2) how it treats reports that have no matching visit row in the window.

The sample data is too small to meet the thresholds, so reason about the logic rather than expecting rows back.

Hints

Hint 1

Never join visits directly to reports. Aggregate each table to its own grain (per ad, per advertiser, or per advertiser-day) and join only the aggregates on a key that is unique on both sides.

Hint 2

Use a half-open window (`>= '2025-08-26'` and `< '2025-09-02'`) instead of `BETWEEN ... '23:59:59'`, and compute advertiser-level distinct reporters with `COUNT(DISTINCT user_id)` over all of the advertiser's ads, not by summing per-ad distinct counts.

Hint 3

Apply the visit and reporter thresholds before the `RANK()` so the rank is among qualifiers. For B use `ROW_NUMBER() OVER (PARTITION BY advertiser_id ...)`, and for C use `RANK() OVER (PARTITION BY day ...)` with a LEFT JOIN from daily visits to daily reports.