Reported Posts - Solution & Explanation
Problem Statement
Table: Actions
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| post_id | int |
| action_date | date |
| action | enum |
| extra | varchar |
+---------------+---------+
This table may have duplicate rows.
The action column is an ENUM (category) type of ('view', 'like', 'reaction', 'comment', 'report', 'share').
The extra column has optional information about the action, such as a reason for the report or a type of reaction.
Write a solution to report the number of posts reported yesterday for each report reason. Assume today is 2019-07-05.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Actions table: +---------+---------+-------------+--------+--------+ | 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 | 4 | 2019-07-04 | view | null | | 2 | 4 | 2019-07-04 | report | spam | | 3 | 4 | 2019-07-04 | view | null | | 3 | 4 | 2019-07-04 | report | spam | | 4 | 3 | 2019-07-02 | view | null | | 4 | 3 | 2019-07-02 | report | spam | | 5 | 2 | 2019-07-04 | view | null | | 5 | 2 | 2019-07-04 | report | racism | | 5 | 5 | 2019-07-04 | view | null | | 5 | 5 | 2019-07-04 | report | racism | +---------+---------+-------------+--------+--------+ Output: +---------------+--------------+ | report_reason | report_count | +---------------+--------------+ | spam | 1 | | racism | 2 | +---------------+--------------+ Explanation: Note that we only care about report reasons with non-zero number of reports.
Approach Overview
Problem Overview: The Actions table logs user interactions with posts. Some rows represent reports where action = 'report' and the extra column stores the report reason (spam, racism, etc.). The task is to compute how many unique posts were reported yesterday for each reason.
Approach 1: Filter + GROUP BY with DISTINCT (O(n) time, O(k) space)
Scan the Actions table and keep only rows where action = 'report' and action_date equals yesterday’s date. Each row represents a report event, but a single post may be reported multiple times for the same reason. Because the problem asks for reported posts, count unique post_id values using COUNT(DISTINCT post_id). Group the filtered rows by the reason stored in extra. The result gives one row per report reason with the number of distinct posts reported for that reason. The query performs a single table scan, making it O(n) time with O(k) space for grouped results, where k is the number of distinct report reasons.
This approach relies on SQL aggregation primitives from SQL and database querying. The key operations are filtering with WHERE, grouping with GROUP BY, and deduplicating posts with COUNT(DISTINCT ...). Databases execute this efficiently using hash aggregation or sorting internally.
Approach 2: Subquery for Distinct Reports then Aggregate (O(n) time, O(n) space)
Another option is to first extract distinct combinations of (post_id, extra) in a subquery after applying the same filters. This step removes duplicate report events early. The outer query then groups the result by extra and counts rows. While logically equivalent, it materializes an intermediate dataset and can use more memory depending on the engine’s execution plan. Time complexity remains O(n) because the table still needs to be scanned.
This variation highlights how deduplication and aggregation can be separated into stages using nested queries. It’s useful when you want explicit control over intermediate transformations or when building more complex reporting pipelines involving GROUP BY operations.
Recommended for interviews: The direct GROUP BY solution with COUNT(DISTINCT post_id). It shows you understand filtering, aggregation, and deduplication in SQL with a single concise query. The subquery version demonstrates the same logic but is typically less direct.
Solution
Code
MySQL
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Filter + GROUP BY with COUNT(DISTINCT) | O(n) | O(k) | Best general solution; concise and optimized for SQL aggregation |
| Subquery with DISTINCT then GROUP BY | O(n) | O(n) | Useful when deduplication must be explicit before aggregation |
Video Solution
LeetCode 1113. Reported Posts • Yuki Kitayama • 45 views views
Frequently Asked Questions
Is Reported Posts easy or hard?
Reported Posts Python/Java solution
How to solve Reported Posts in O(n)?
What is the best approach for Reported Posts?
Is Reported Posts asked at Google/Amazon/Meta?
What data structure is used in Reported Posts?
What is the time complexity of Reported Posts?
Ready to solve this problem?
Practice Reported Posts with our built-in code editor and test cases.
Practice on FleetCode