Content Flagging System Metrics and Abuse Analysis
SQL & DataAuto-graded · 17 test casesLast updated
Process moderation report logs to calculate unique report ratios and aggregate key reporting metrics per reporter.
You are tasked with analyzing moderation report logs to assess the health of a content flagging system and detect potential system abuse.
Given a list of dictionary entries representing submission logs, write a function that calculates platform-wide reported-user metrics and per-reporter accuracy statistics.
Input Format
Each dictionary in reports represents a single report entry containing:
report_id(str): Unique identifier for the report.reporter_id(str): Unique identifier for the user submitting the report.reported_user_id(str): Unique identifier for the target user being reported.is_valid(bool):Trueif moderation confirmed a violation,Falseotherwise.day(str): Date string formatted asYYYY-MM-DD.
Processing Rules
- Deduplication: Duplicate logs sharing an identical
report_idcan appear. Keep only the first occurrence of eachreport_idand ignore subsequent duplicates. - Platform Metrics:
reported_user_count: Count of uniquereported_user_ids across all deduplicated reports.valid_reported_user_count: Count of uniquereported_user_ids who have at least one deduplicated report whereis_validisTrue.valid_reported_user_share: Ratiovalid_reported_user_count / reported_user_countas a float. Ifreported_user_countis 0, this value should be0.0.
- Per-Reporter Metrics (
reporter_metrics):- For every distinct
reporter_idpresent in the deduplicated logs, generate a summary record. - Summaries must be sorted in ascending lexicographical order by
reporter_id. - Each reporter summary must contain:
reporter_id(str): Identifier of the reporter.report_count(int): Total number of valid unique reports submitted by this reporter.valid_report_count(int): Total number of valid unique reports submitted by this reporter whereis_validisTrue.hit_rate(float): Ratiovalid_report_count / report_count.active_days(int): Total number of distinct calendardays on which this reporter submitted at least one report.reports_per_active_day(float): Ratioreport_count / active_days.
- For every distinct
Return Value
Return a dictionary with the following schema:
{
"valid_reported_user_share": float,
"reported_user_count": int,
"valid_reported_user_count": int,
"reporter_metrics": [
{
"reporter_id": str,
"report_count": int,
"valid_report_count": int,
"hit_rate": float,
"active_days": int,
"reports_per_active_day": float,
},
...
],
}
Constraints
0 <= len(reports) <= 10,000- All key fields (
report_id,reporter_id,reported_user_id,is_valid,day) are guaranteed to exist in each record.
Examples
Example 1
Input: analyze_reporting_abuse([{'report_id': 'r1', 'reporter_id': 'alice', 'reported_user_id': 'u1', 'is_valid': True, 'day': '2024-01-01'}, {'report_id': 'r2', 'reporter_id': 'alice', 'reported_user_id': 'u2', 'is_valid': False, 'day': '2024-01-01'}, {'report_id': 'r3', 'reporter_id': 'bob', 'reported_user_id': 'u2', 'is_valid': True, 'day': '2024-01-02'}])
Output: {'valid_reported_user_share': 1, 'reported_user_count': 2, 'valid_reported_user_count': 2, 'reporter_metrics': [{'reporter_id': 'alice', 'report_count': 2, 'valid_report_count': 1, 'hit_rate': 0.5, 'active_days': 1, 'reports_per_active_day': 2}, {'reporter_id': 'bob', 'report_count': 1, 'valid_report_count': 1, 'hit_rate': 1, 'active_days': 1, 'reports_per_active_day': 1}]}
Example 2
Input: analyze_reporting_abuse([{'report_id': 'r1', 'reporter_id': 'alice', 'reported_user_id': 'u1', 'is_valid': False, 'day': '2024-01-01'}, {'report_id': 'r1', 'reporter_id': 'alice', 'reported_user_id': 'u1', 'is_valid': False, 'day': '2024-01-01'}, {'report_id': 'r2', 'reporter_id': 'alice', 'reported_user_id': 'u1', 'is_valid': True, 'day': '2024-01-02'}])
Output: {'valid_reported_user_share': 1, 'reported_user_count': 1, 'valid_reported_user_count': 1, 'reporter_metrics': [{'reporter_id': 'alice', 'report_count': 2, 'valid_report_count': 1, 'hit_rate': 0.5, 'active_days': 2, 'reports_per_active_day': 1}]}
Example 3
Input: analyze_reporting_abuse([{'report_id': 'r1', 'reporter_id': 'alice', 'reported_user_id': 'u1', 'is_valid': False, 'day': '2024-01-01'}, {'report_id': 'r2', 'reporter_id': 'alice', 'reported_user_id': 'u2', 'is_valid': False, 'day': '2024-01-01'}, {'report_id': 'r3', 'reporter_id': 'alice', 'reported_user_id': 'u3', 'is_valid': False, 'day': '2024-01-02'}])
Output: {'valid_reported_user_share': 0, 'reported_user_count': 3, 'valid_reported_user_count': 0, 'reporter_metrics': [{'reporter_id': 'alice', 'report_count': 3, 'valid_report_count': 0, 'hit_rate': 0, 'active_days': 2, 'reports_per_active_day': 1.5}]}
Example 4
Input: analyze_reporting_abuse([{'report_id': 'r1', 'reporter_id': 'alice', 'reported_user_id': 'u1', 'is_valid': True, 'day': '2024-01-01'}, {'report_id': 'r2', 'reporter_id': 'bob', 'reported_user_id': 'u1', 'is_valid': True, 'day': '2024-01-01'}, {'report_id': 'r3', 'reporter_id': 'bob', 'reported_user_id': 'u2', 'is_valid': True, 'day': '2024-01-03'}])
Output: {'valid_reported_user_share': 1, 'reported_user_count': 2, 'valid_reported_user_count': 2, 'reporter_metrics': [{'reporter_id': 'alice', 'report_count': 1, 'valid_report_count': 1, 'hit_rate': 1, 'active_days': 1, 'reports_per_active_day': 1}, {'reporter_id': 'bob', 'report_count': 2, 'valid_report_count': 2, 'hit_rate': 1, 'active_days': 2, 'reports_per_active_day': 1}]}
Hints
Hint 1
Filter out duplicate report records first by storing seen `report_id` values in a set.
Hint 2
Iterate through the deduplicated list to maintain sets of unique target users and per-reporter stats (counts, valid counts, active days).
Hint 3
Ensure reporters in the output list are sorted alphabetically by their `reporter_id`.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem asks us to compute aggregated statistics over moderation reports. We can achieve this in three main steps:
-
Deduplication: We iterate through the input list of reports while keeping track of seen
report_ids in a hash set. Only the first occurrence of each uniquereport_idis processed. -
Aggregation: We perform a single pass over the unique reports to populate:
- A global set of all reported target user IDs.
- A global set of target user IDs that have at least one valid report.
- A map keyed by
reporter_idstoring total reports, valid reports, and a set of distinct calendar days active.
-
Metric Calculation and Sorting:
- Calculate
valid_reported_user_shareaslen(valid_reported_users) / len(reported_users). Handle division by zero by defaulting to0.0. - Iterate through the aggregated reporter data sorted by
reporter_idlexicographically. - For each reporter, calculate
hit_rate(valid_report_count / report_count) andreports_per_active_day(report_count / active_days).
- Calculate
Complexity Analysis
- Time Complexity: $O(N + M \log M)$, where $N$ is the number of input reports and $M$ is the number of distinct reporters. Scanning through $N$ records takes $O(N)$ time, and sorting the $M$ unique reporters takes $O(M \log M)$ time.
- Space Complexity: $O(N)$ to store unique reports, target user IDs, and aggregated reporter sets.
from typing import List, Dict
def analyze_reporting_abuse(reports: List[Dict]) -> Dict:
unique_reports = []
seen_ids = set()
for r in reports:
rid = r["report_id"]
if rid not in seen_ids:
seen_ids.add(rid)
unique_reports.append(r)
all_reported_users = set()
valid_reported_users = set()
reporter_data = {}
for r in unique_reports:
target = r["reported_user_id"]
reporter = r["reporter_id"]
is_valid = bool(r["is_valid"])
day = r["day"]
all_reported_users.add(target)
if is_valid:
valid_reported_users.add(target)
if reporter not in reporter_data:
reporter_data[reporter] = {
"count": 0,
"valid_count": 0,
"days": set()
}
reporter_data[reporter]["count"] += 1
if is_valid:
reporter_data[reporter]["valid_count"] += 1
reporter_data[reporter]["days"].add(day)
total_reported = len(all_reported_users)
valid_reported = len(valid_reported_users)
share = (valid_reported / total_reported) if total_reported > 0 else 0.0
metrics_list = []
for reporter in sorted(reporter_data.keys()):
data = reporter_data[reporter]
rc = data["count"]
vc = data["valid_count"]
days_cnt = len(data["days"])
metrics_list.append({
"reporter_id": reporter,
"report_count": rc,
"valid_report_count": vc,
"hit_rate": vc / rc,
"active_days": days_cnt,
"reports_per_active_day": rc / days_cnt
})
return {
"valid_reported_user_share": share,
"reported_user_count": total_reported,
"valid_reported_user_count": valid_reported,
"reporter_metrics": metrics_list
}