Compute Moderation View Metrics
AirwallexData Scientist · OnsiteLast updated
An Airwallex Data Scientist onsite SQL question on content-moderation analytics. You write three queries over a per-day view table and a one-to-many post/violation-type policy-score table: distinct posts with 10+ views in the last 7 days, Nudity view prevalence over the last 30 days, and a monthly violation-type distribution. The core challenge is windowed aggregation plus avoiding view double-counting across violation types.
Question
You are working on a content moderation analytics problem. You have two tables.
Table 1: content_views (one row per user / post / day)
user_idINTEGER: user who viewed the postpost_idINTEGER: viewed post IDview_countINTEGER: number of views that user generated for that post on that dateview_dateSTRING: date of the views inYYYY-MM-DDformat- Primary key: (
user_id,post_id,view_date)
Table 2: post_policy_scores (one row per post / violation type)
post_idINTEGER: post IDviolation_typeSTRING: policy category such asSpam,Scam,Nudity, orHarassmentprobability_violatingDOUBLE: model-estimated probability that the post violates that policy- Primary key: (
post_id,violation_type)
Relationship: one post can have multiple rows in post_policy_scores, one per possible violation type. content_views.post_id joins to post_policy_scores.post_id.
Assumptions
- Treat
view_dateas a UTC date string and cast it toDATEwhen needed. - Let
analysis_datebe the maximumview_datepresent incontent_views. - The last 7 days and last 30 days are inclusive windows ending on
analysis_date. - For classification-based questions, treat a post as belonging to a violation type when
probability_violating > 0.5. - For monthly aggregation, use the calendar month derived from
view_date.
Write SQL for the following tasks:
-
Posts with at least 10 views (last 7 days). Count how many distinct posts received at least 10 total views in the last 7 days. Aggregate
view_countacross all users and all dates in the window, and apply the 10-view filter after grouping bypost_id. Required output column:num_posts. -
Nudity view prevalence (last 30 days). Define a post as a
Nuditypost if it has apost_policy_scoresrow withviolation_type = 'Nudity'andprobability_violating > 0.5. Compute:view prevalence = total views on Nudity posts in the last 30 days / total views on all posts in the last 30 days. Required output columns:nudity_views,total_views,nudity_view_prevalence. -
Monthly violation-type distribution. For each calendar month (derived from
view_date) and eachviolation_type, among post/violation pairs whereprobability_violating > 0.5and the post received at least one view in that month, report the number of distinct posts assigned to that violation type and that type's share of all flagged post/violation pairs in the month. Required output columns:month,violation_type,flagged_post_count,distribution_pct.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.