KatInterview

Query Engine for Content Moderation Event Logs

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designhash-mapbinary-searchindexing

Last updated

Build an efficient query system over sorted violation records supporting policy lookups, post lookups, and time-based aggregations using indexing and binary search.

You need to build a query engine for a content moderation system. The engine receives a chronologically ordered log of moderation events, where each event is a triple [post_id, policy, timestamp]. The log is sorted in non-decreasing order by timestamp.

Your task is to process this log and answer various queries efficiently. Implement a function that takes the log and a series of query operations, then returns the results of all queries in order.

The system must support five types of queries:

  1. count_unique_posts_by_policy with argument [policy]: Return how many different posts have ever violated the given policy.

  2. get_policies_for_post with argument [post_id]: Return a sorted list of all distinct policies that this post has violated. If the post has no violations, return an empty list.

  3. count_unique_posts_at_time with argument [timestamp]: Return how many different posts have at least one violation record at exactly this timestamp.

  4. count_unique_posts_in_range with arguments [start_time, end_time]: Return how many different posts have at least one violation within the inclusive time range [start_time, end_time].

  5. count_violations_by_policy_in_range with arguments [policy, start_time, end_time]: Return the total number of violation records for the specified policy within the inclusive time range. Count each record separately, even if the same post violates the policy multiple times.

Constraints

  • 1 <= len(logs) <= 10^5
  • post_id and policy are non-empty alphanumeric strings (including underscores) with length <= 64
  • 0 <= timestamp <= 10^14
  • The logs array is sorted by timestamp in non-decreasing order
  • start_time <= end_time for all range queries
  • Up to 2 * 10^4 total query operations will be executed
  • Queries may reference policies or posts that do not exist in the log

Examples

Example 1

Input: content_moderation_query_engine([['post_1', 'spam', 100], ['post_2', 'copyright', 100], ['post_1', 'hate_speech', 150], ['post_3', 'spam', 200], ['post_1', 'spam', 250]], ['count_unique_posts_by_policy', 'get_policies_for_post', 'count_unique_posts_at_time', 'count_unique_posts_in_range', 'count_violations_by_policy_in_range'], [['spam'], ['post_1'], [100], [100, 150], ['spam', 100, 250]])

Output: [2, ['hate_speech', 'spam'], 2, 2, 3]

Hints

Hint 1

Build indexes during initialization to make each query fast—aggregate the data once rather than scanning repeatedly.

Hint 2

The log is already sorted by time, so you can use binary search for range queries without additional sorting.

Hint 3

For counting distinct posts in a time range, a post might appear at multiple timestamps within that range, so you need to track uniqueness across all matching records.