Write SQL for Moderation Metrics
Problem Description
Assume the following two tables are available:
post_views
user_idINTEGER: ID of the user who viewed the postpost_idINTEGER: ID of the postview_countINTEGER: Number of views generated by that user for that post on that dateview_dateSTRING: View date inYYYY-MM-DDformat
Primary key: (user_id, post_id, view_date)
post_policy_scores
post_idINTEGER: ID of the postviolation_typeSTRING: Policy category such asSpam,Scam,Nudity, orHarassmentprobability_violatingDOUBLE: Model-estimated probability that the post violates the policy for that violation type
Primary key: (post_id, violation_type)
Relationship: One post can have multiple rows in post_policy_scores, one per possible violation type.
Assumptions:
- All time windows are inclusive.
- Dates should be interpreted in UTC.
- The reference date for all calculations is the maximum
view_datefound in thepost_viewstable.
Tasks
Write SQL queries to perform the following tasks:
-
Count Distinct Posts with High Views (Last 7 Days):
- Count how many distinct posts have at least 10 total views in the last 7 days. The
view_countshould be aggregated across all users and all dates within the 7-day window. - Required output column:
posts_with_at_least_10_views_7d.
- Count how many distinct posts have at least 10 total views in the last 7 days. The
-
Compute Nudity View Prevalence (Last 30 Days):
- Compute the view prevalence of
Nudityin the last 30 days. - A post is defined as a
Nuditypost ifviolation_type = 'Nudity'andprobability_violating > 0.5. - View prevalence is defined as:
(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_30d,total_views_30d,nudity_view_prevalence_30d.
- Compute the view prevalence of
-
Compute Monthly Violation-Type Distribution:
- For each calendar month derived from
view_date:- Count the number of distinct posts viewed in that month that are classified as violating under the rule
probability_violating > 0.5, grouped byviolation_type. - Then, compute each
violation_type's share of all violating posts viewed in that month.
- Count the number of distinct posts viewed in that month that are classified as violating under the rule
- Required output columns:
month,violation_type,violating_post_count,monthly_violation_share.
- For each calendar month derived from
AI interview prep
Core dilemma
Accurately combining temporal filtering, conditional logic, and multi-level aggregations across disparate datasets to derive complex metrics efficiently within SQL.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Write SQL for Moderation Metrics
Problem Description
Assume the following two tables are available:
post_views
user_idINTEGER: ID of the user who viewed the postpost_idINTEGER: ID of the postview_countINTEGER: Number of views generated by that user for that post on that dateview_dateSTRING: View date inYYYY-MM-DDformat
Primary key: (user_id, post_id, view_date)
post_policy_scores
post_idINTEGER: ID of the postviolation_typeSTRING: Policy category such asSpam,Scam,Nudity, orHarassmentprobability_violatingDOUBLE: Model-estimated probability that the post violates the policy for that violation type
Primary key: (post_id, violation_type)
Relationship: One post can have multiple rows in post_policy_scores, one per possible violation type.
Assumptions:
- All time windows are inclusive.
- Dates should be interpreted in UTC.
- The reference date for all calculations is the maximum
view_datefound in thepost_viewstable.
Tasks
Write SQL queries to perform the following tasks:
-
Count Distinct Posts with High Views (Last 7 Days):
- Count how many distinct posts have at least 10 total views in the last 7 days. The
view_countshould be aggregated across all users and all dates within the 7-day window. - Required output column:
posts_with_at_least_10_views_7d.
- Count how many distinct posts have at least 10 total views in the last 7 days. The
-
Compute Nudity View Prevalence (Last 30 Days):
- Compute the view prevalence of
Nudityin the last 30 days. - A post is defined as a
Nuditypost ifviolation_type = 'Nudity'andprobability_violating > 0.5. - View prevalence is defined as:
(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_30d,total_views_30d,nudity_view_prevalence_30d.
- Compute the view prevalence of
-
Compute Monthly Violation-Type Distribution:
- For each calendar month derived from
view_date:- Count the number of distinct posts viewed in that month that are classified as violating under the rule
probability_violating > 0.5, grouped byviolation_type. - Then, compute each
violation_type's share of all violating posts viewed in that month.
- Count the number of distinct posts viewed in that month that are classified as violating under the rule
- Required output columns:
month,violation_type,violating_post_count,monthly_violation_share.
- For each calendar month derived from