KatInterview

Content Flagging System Metrics and Abuse Analysis

SQL & DataAuto-graded · 17 test cases
EasyAuto-gradedSQL & Dataarraysdictionariesdata-processing

Last 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): True if moderation confirmed a violation, False otherwise.
  • day (str): Date string formatted as YYYY-MM-DD.

Processing Rules

  1. Deduplication: Duplicate logs sharing an identical report_id can appear. Keep only the first occurrence of each report_id and ignore subsequent duplicates.
  2. Platform Metrics:
    • reported_user_count: Count of unique reported_user_ids across all deduplicated reports.
    • valid_reported_user_count: Count of unique reported_user_ids who have at least one deduplicated report where is_valid is True.
    • valid_reported_user_share: Ratio valid_reported_user_count / reported_user_count as a float. If reported_user_count is 0, this value should be 0.0.
  3. Per-Reporter Metrics (reporter_metrics):
    • For every distinct reporter_id present 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 where is_valid is True.
      • hit_rate (float): Ratio valid_report_count / report_count.
      • active_days (int): Total number of distinct calendar days on which this reporter submitted at least one report.
      • reports_per_active_day (float): Ratio report_count / active_days.

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`.