KatInterview

Exposure Rate of Violating Content: Ex-ante vs Ex-post Views

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionsas-of-join

Last updated

Write SQL that labels each view by the moderation decision known at view time and by the final decision, computes daily prevalence per country, and ranks the top creators by violating share.

You work on analytics for a content platform and need to measure how often people see policy-violating content. Three tables are available:

  • content_views(view_ts TIMESTAMP, user_id INT, content_id INT, surface VARCHAR, country VARCHAR)
  • content_moderation(content_id INT, decision VARCHAR, violation_type VARCHAR, detected_ts TIMESTAMP, source VARCHAR, is_removed BOOLEAN)
  • content(content_id INT, creator_id INT, created_ts TIMESTAMP, country VARCHAR)

A content item can have several moderation rows over time, for example a VIOLATION later overturned by a CLEAN.

Treat "today" as 2025-09-01. The reporting window is the last 7 days, 2025-08-26 through 2025-09-01 inclusive.

Part 1: daily prevalence. For each (view_date, country), where country is the viewer's country from content_views, return:

view_date, country, total_views, violating_views_ex_ante, violating_views_ex_post, ex_ante_prevalence, ex_post_prevalence

  • Ex-ante: a view is violating only if the most recent decision with detected_ts <= view_ts is VIOLATION. If the content has no decision at that moment, treat it as CLEAN.
  • Ex-post: a view is violating if the content's latest decision overall (any detected_ts) is VIOLATION.
  • Prevalence is violating views divided by total views.

Part 2: worst creators. Over the same window, find the top 3 creators by ex-post violating_view_share, defined as the creator's ex-post violating views divided by all ex-post violating views in the window. For each of them, break their violating views down by surface.

Sample data:

content

content_idcreator_idcreated_tscountry
10110012025-08-25 10:00:00US
10210022025-08-30 12:00:00US
10310032025-08-31 08:00:00CA

content_views

view_tsuser_idcontent_idsurfacecountry
2025-08-26 09:00:00504101feedUS
2025-08-30 18:00:00501102feedUS
2025-08-31 20:00:00501101feedUS
2025-09-01 09:00:00502102feedUS
2025-09-01 10:00:00503103searchCA

content_moderation

content_iddecisionviolation_typedetected_tssourceis_removed
101VIOLATIONspam2025-08-31 21:00:00humantrue
102CLEANNULL2025-08-30 14:00:00modelfalse
103VIOLATIONhate_speech2025-09-01 12:00:00modeltrue

Write the SQL (any mainstream dialect, PostgreSQL preferred), explain the choices that affect correctness, and state what your queries return on the sample.

Hints

Hint 1

Treat each view as the unit of analysis. Prevalence counts views, not distinct content items, so every view needs its own ex-ante label and ex-post label.

Hint 2

The ex-ante label is an as-of lookup: for each view, take the newest moderation row at or before view_ts. A LEFT JOIN LATERAL with ORDER BY ... LIMIT 1 (or ROW_NUMBER over a range join) does this, and a missing row maps to CLEAN.

Hint 3

Filter the window with view_ts >= '2025-08-26' and view_ts < '2025-09-02'. BETWEEN with a date literal on the upper end silently drops most of the last day. Also break ties on identical detected_ts explicitly.