KatInterview

Trust & Safety Exposure Queries on Daily Post Views

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionsjoins

Last updated

Write three SQL queries over daily post views and per-policy risk scores: a windowed view threshold, nudity exposure share, and a monthly flagged-type mix.

Context

A Trust & Safety analytics team keeps two tables. Your job is to answer three exposure questions with SQL. The main difficulties are date windows, joins that can multiply rows, and deciding exactly what the denominator should count.

Table 1: daily_post_views (one row per user, post, and day)

  • user_id INTEGER: the user who viewed the post
  • post_id INTEGER: the post that was viewed
  • views INTEGER: number of views that user generated on that post on that day
  • view_date STRING in YYYY-MM-DD format, treated as a UTC date
  • Primary key: (user_id, post_id, view_date)

Table 2: policy_risk (one row per post and policy)

  • post_id INTEGER
  • policy STRING, such as Spam, Scam, Nudity, or Harassment
  • risk_prob DOUBLE: model-estimated probability that the post violates that policy
  • Primary key: (post_id, policy)

A single post can have several policy_risk rows, one per policy. daily_post_views.post_id joins to policy_risk.post_id.

Assumptions

  • Cast view_date to DATE when you need date arithmetic.
  • Define as_of as the maximum view_date in daily_post_views. Do not use the current system date.
  • "Last 7 days" and "last 30 days" are inclusive windows that end on as_of.
  • A post belongs to a policy when risk_prob > 0.5. The comparison is strict.
  • Months come from the calendar month of view_date.

Tasks

1. Posts with at least 10 views in the last 7 days. Count the distinct posts whose total views across all users and all dates in the window are at least 10. Apply the threshold to the post total after grouping. Output column: num_posts.

2. Nudity view prevalence in the last 30 days. A post counts as a Nudity post if it has a policy_risk row with policy = 'Nudity' and risk_prob > 0.5. Compute:

prevalence = views on Nudity posts in the last 30 days / views on all posts in the last 30 days

Output columns: nudity_views, total_views, nudity_view_prevalence.

3. Monthly policy distribution. For each calendar month and each policy, consider only post/policy pairs with risk_prob > 0.5 where the post received at least one view in that month. Report how many distinct posts were assigned to that policy in that month, and that policy's share of all flagged pairs in the month. Output columns: month, violation_type, flagged_post_count, distribution_pct.

Write standard SQL and note any dialect-specific functions you use.

Hints

Hint 1

Compute the anchor date once in a CTE. For an inclusive N-day window, the lower bound is as_of minus (N-1) days, not minus N days.

Hint 2

Filtering on a summed view count belongs in HAVING, not WHERE. For Q2, pre-reduce the risk table to one row per post before you join, or use EXISTS, so the join cannot multiply view rows.

Hint 3

For Q3, deduplicate to one row per (month, post) before counting. The percentage denominator is the total number of flagged pairs in the month, which you can get with a window SUM partitioned by month after grouping.