KatInterview

Banned-Word Review Screener with Per-User Strike Counts

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designhash-sethash-mapsimulation

Last updated

Simulate a stateful screening service driven by an operation log: whole-token, case-insensitive banned-word checks, replaceable word lists, and per-user flagged-review tallies.

Banned-Word Review Screener

A shopping site wants to screen user-submitted product reviews. A review is flagged when it contains at least one banned word. The service also keeps a tally of how many of each user's reviews have been flagged. The list of banned words can be swapped out while the service is running.

Instead of calling a class directly, your function receives a log of operations and must replay it, returning one result per operation.

Operations

Each operation is a name in operations, paired with an argument list at the same index in arguments.

OperationArgumentsEffectResult
"init"[words]Start a fresh service whose banned list is words. All previous state, including every user's tally, is discarded.None
"process_review"[user_id, review_text]Decide whether the review is flagged. If it is, add 1 to that user's tally.True if flagged, else False
"get_violation_count"[user_id]Look up how many of this user's reviews have been flagged so far.the tally (an int, 0 if the user has none)
"update_prohibited_words"[new_words]Replace the current banned list with new_words (do not merge). Tallies are kept as they are.None

Matching rules

  • Split review_text into tokens on whitespace. Punctuation is not removed: it stays part of the token. For example, in "This is spam." the token is "spam.", which does not match the banned word "spam".
  • Matching is case-insensitive: "Spam", "SPAM" and "spam" are the same word (use Unicode case folding).
  • A token matches only if it equals a banned word in full. Substrings never match, so banning "spam" does not flag "spammy", unless "spammy" is itself banned.
  • A review is flagged if at least one token matches. A review with several banned tokens still adds only 1 to the tally.
  • An empty or whitespace-only review has no tokens and is never flagged.
  • Every process_review uses the banned list that is current at that moment.
  • If an operation other than the four above appears, raise ValueError.
  • Before any "init", the banned list is empty and no tallies exist.

Task

Implement screen_reviews(operations, arguments) that replays the log and returns the list of results, in order, one per operation.

Constraints

  • len(operations) == len(arguments).
  • user_id values and review texts are strings; banned words are strings.
  • The banned list may be empty.
  • Processing a review should take time linear in its length, independent of the number of banned words.

Examples

Example 1

Input: screen_reviews(['init', 'process_review', 'process_review', 'get_violation_count'], [[['spam', 'bot']], ['user1', 'This is a great product'], ['user1', 'This is spam'], ['user1']])

Output: [None, False, True, 1]

Hints

Hint 1

Keep two pieces of state: a collection of banned words that supports fast membership tests, and a mapping from user to the number of flagged reviews.

Hint 2

Normalize case once when a banned list is stored, and normalize each token when checking it. Use plain whitespace splitting and do not strip punctuation.

Hint 3

Stop at the first matching token, since each review adds at most 1. Remember that `init` clears tallies, while `update_prohibited_words` replaces only the word list and keeps tallies.