Reported Posts - Problem

You're building a content moderation system for a social media platform. The platform tracks all user interactions with posts in an Actions table, including views, likes, comments, and most importantly - reports.

Your mission: Generate a daily report showing how many posts were reported yesterday, grouped by the reason for reporting. This helps the moderation team understand what types of content issues are most common.

Table: Actions

Column NameType
user_idint
post_idint
action_datedate
actionenum
extravarchar

The action column contains values like 'view', 'like', 'reaction', 'comment', 'report', 'share'. When action = 'report', the extra column contains the reason for the report (e.g., 'spam', 'violence', 'hate speech').

Given: Today is 2019-07-05

Return: Number of posts reported on 2019-07-04, grouped by report reason, in any order.

Input & Output

example_1.sql โ€” Basic Report Counting
$ Input: Actions table: | user_id | post_id | action_date | action | extra | |---------|---------|-------------|--------|----------| | 1 | 1 | 2019-07-04 | view | null | | 1 | 1 | 2019-07-04 | report | spam | | 1 | 1 | 2019-07-05 | view | null | | 2 | 2 | 2019-07-04 | report | spam | | 2 | 2 | 2019-07-04 | report | violence | | 3 | 4 | 2019-07-04 | view | null |
โ€บ Output: | report_reason | report_count | |---------------|---------------| | spam | 2 | | violence | 1 |
๐Ÿ’ก Note: On 2019-07-04 (yesterday), there were 2 posts reported for 'spam' (post_id 1 and 2) and 1 post reported for 'violence' (post_id 2). Note that post_id 2 was reported for both reasons, so it appears in both counts.
example_2.sql โ€” No Reports Yesterday
$ Input: Actions table: | user_id | post_id | action_date | action | extra | |---------|---------|-------------|--------|---------| | 1 | 1 | 2019-07-03 | report | spam | | 1 | 1 | 2019-07-05 | report | spam | | 2 | 2 | 2019-07-04 | view | null |
โ€บ Output: Empty result set: | report_reason | report_count | |---------------|---------------|
๐Ÿ’ก Note: No posts were reported on 2019-07-04 (yesterday). The reports happened on 2019-07-03 and 2019-07-05, so our result is empty.
example_3.sql โ€” Multiple Reports Same Post
$ Input: Actions table: | user_id | post_id | action_date | action | extra | |---------|---------|-------------|--------|-----------| | 1 | 1 | 2019-07-04 | report | spam | | 2 | 1 | 2019-07-04 | report | spam | | 3 | 1 | 2019-07-04 | report | violence | | 4 | 2 | 2019-07-04 | report | spam |
โ€บ Output: | report_reason | report_count | |---------------|---------------| | spam | 2 | | violence | 1 |
๐Ÿ’ก Note: Post 1 was reported by 3 different users (2 for spam, 1 for violence), but we count distinct posts, so 'spam' gets count 2 (posts 1 and 2) and 'violence' gets count 1 (post 1). Multiple reports of the same post for the same reason don't inflate the count.

Visualization

Tap to expand
๐Ÿ“ฑ Social Media Content Moderation Dashboard๐Ÿ‘คUser 1Reports:"This is spam"๐Ÿ‘คUser 2Reports:"Violence"๐Ÿ‘คUser 3Reports:"Spam again"๐Ÿ—ƒ๏ธ Actions TableAll reports loggedwith timestampsโšก SQL MagicWHERE + GROUP BY+ COUNT DISTINCT๐Ÿ“Š Daily Moderation ReportReport ReasonCountspam2violence1๐Ÿ‘จโ€๐Ÿ’ผModerator"Prioritize spam!"๐ŸŽฏ Key Insight: One SQL query replaces complex manual processes!
Understanding the Visualization
1
User Reports
Throughout the day, users report posts for various reasons (spam, violence, hate speech, etc.)
2
Data Collection
All reports are logged in the Actions table with timestamps and reasons
3
Daily Filter
At the end of each day, we filter for yesterday's reports only
4
Smart Grouping
Group reports by reason and count unique posts (not duplicate reports)
5
Moderation Report
Present clean summary to moderation team for prioritization
Key Takeaway
๐ŸŽฏ Key Insight: Database aggregation functions like GROUP BY and COUNT(DISTINCT) are specifically designed for this type of analysis. Let the database do what it does best - filtering, grouping, and counting large datasets efficiently!

Time & Space Complexity

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

Linear scan with database optimizations, potentially much faster with indexes on action_date and action columns

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Where k is the number of unique report reasons for grouping results

n
2n
โœ“ Linear Space

Constraints

  • The Actions table may contain duplicate rows
  • action is an ENUM with values: 'view', 'like', 'reaction', 'comment', 'report', 'share'
  • extra column is optional and contains additional information like report reasons
  • Today's date is fixed as 2019-07-05, so yesterday is 2019-07-04
  • Return results in any order
  • If no reports found for yesterday, return empty result set
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
23.4K Views
High Frequency
~12 min Avg. Time
856 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