Reported Posts II - Problem

You're working as a content moderator for a social media platform, analyzing the effectiveness of your spam detection system. Your goal is to calculate the daily removal rate of posts that were reported as spam.

You have access to two tables:

  • Actions: Records all user interactions with posts, including reports
  • Removals: Records when posts were actually removed by moderators

Your task is to find the average daily percentage of spam-reported posts that got removed, rounded to 2 decimal places.

Key Challenge: You need to match posts that were both reported as spam AND subsequently removed, then calculate the daily removal rate across all days.

Think of it like measuring how effective your content moderation team is at acting on spam reports!

Input & Output

basic_example.sql โ€” Basic Case
$ Input: Actions: | user_id | post_id | action_date | action | extra | |---------|---------|-------------|--------|---------| | 1 | 1 | 2019-07-01 | view | null | | 1 | 1 | 2019-07-01 | like | null | | 1 | 1 | 2019-07-01 | share | null | | 2 | 2 | 2019-07-01 | report | spam | | 2 | 3 | 2019-07-02 | report | spam | Removals: | post_id | remove_date | |---------|-------------| | 2 | 2019-07-02 |
โ€บ Output: | average_daily_percent | |-----------------------| | 50.00 |
๐Ÿ’ก Note: Day 1: 1 spam report (post_id=2), 1 removed โ†’ 100%. Day 2: 1 spam report (post_id=3), 0 removed โ†’ 0%. Average = (100 + 0) / 2 = 50.00%
no_removals.sql โ€” No Spam Removals
$ Input: Actions: | user_id | post_id | action_date | action | extra | |---------|---------|-------------|--------|---------| | 1 | 1 | 2019-07-01 | report | spam | | 2 | 2 | 2019-07-01 | report | spam | Removals: | post_id | remove_date | |---------|-------------|
โ€บ Output: | average_daily_percent | |-----------------------| | 0.00 |
๐Ÿ’ก Note: 2 posts reported as spam on 2019-07-01, but none were removed. Daily percentage = 0/2 * 100 = 0.00%
all_removed.sql โ€” All Spam Removed
$ Input: Actions: | user_id | post_id | action_date | action | extra | |---------|---------|-------------|--------|---------| | 1 | 1 | 2019-07-01 | report | spam | | 2 | 2 | 2019-07-01 | report | spam | | 3 | 3 | 2019-07-02 | report | spam | Removals: | post_id | remove_date | |---------|-------------| | 1 | 2019-07-02 | | 2 | 2019-07-03 | | 3 | 2019-07-02 |
โ€บ Output: | average_daily_percent | |-----------------------| | 100.00 |
๐Ÿ’ก Note: Day 1: 2 reports, both removed โ†’ 100%. Day 2: 1 report, removed โ†’ 100%. Average = (100 + 100) / 2 = 100.00%

Visualization

Tap to expand
๐Ÿ“ข Daily ReportsDay 1: 3 postsDay 2: 2 postsDay 3: 1 post๐Ÿ—‘๏ธ RemovalsDay 1: 2 removedDay 2: 2 removedDay 3: 0 removed๐Ÿ“Š Daily %Day 1: 66.67%Day 2: 100%Day 3: 0%๐ŸŽฏ Average55.56%JOINCalculateAverage
Understanding the Visualization
1
Daily Spam Reports
Collect all unique posts reported as spam each day
2
Match with Removals
Check which reported posts were actually removed
3
Calculate Daily Rate
For each day: (removed posts / reported posts) ร— 100
4
Average Performance
Average all daily percentages to get overall effectiveness
Key Takeaway
๐ŸŽฏ Key Insight: Group by date first to calculate daily percentages correctly, then average those percentages. This approach handles varying daily volumes and provides meaningful effectiveness metrics.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single scan of Actions table plus efficient JOIN operation with Removals table

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space needed for intermediate CTE results and JOIN operation

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค user_id, post_id โ‰ค 105
  • action_date and remove_date are valid dates
  • action โˆˆ {'view', 'like', 'reaction', 'comment', 'report', 'share'}
  • extra field is 'spam' only for report actions we care about
  • A post can be reported multiple times but should be counted once per day
Asked in
Meta 45 Google 38 Twitter 32 LinkedIn 28
28.4K Views
High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen