Implement Review Content Moderation
AmazonSoftware Engineer · Online AssessmentLast updated
This problem tests your ability to design a content moderation service that efficiently checks for prohibited words and maintains a running count of violations per user using a stateful metadata store.
Problem Statement
You are building a moderation service for a product review platform. Your task is to process incoming review text and determine if it should be flagged based on a set of prohibited words.
Requirements
- Moderation Check: A review is considered 'flagged' if it contains one or more words from a dynamic list of
prohibited_words. - Stateful Tracking: You must track the total number of flagged reviews submitted by each
user_id. - Persistence/Update: The list of
prohibited_wordscan be updated at runtime. The system must use the current list for every incoming check.
Implement a class ReviewModerator with the following methods:
__init__(self, prohibited_words): Initialize the moderator with a set of prohibited words.process_review(self, user_id, review_text): ReturnsTrueif the review contains any prohibited words,Falseotherwise. If flagged, increment the user's violation count.get_violation_count(self, user_id): Return the total number of flagged reviews for a given user.update_prohibited_words(self, new_words): Update the master list of prohibited words.
Constraints
review_textcan contain punctuation; treat words as case-insensitive sequences separated by whitespace.user_idis a string.- Efficiency: The
process_reviewmethod should be optimized for high-frequency calls, as review volume is high.
Example
moderator = ReviewModerator({"spam", "bot"})
moderator.process_review("user1", "This is a great product") # Returns False
moderator.process_review("user1", "This is spam") # Returns True
moderator.get_violation_count("user1") # Returns 1
Edge Cases
- Empty
review_text. - Overlapping prohibited words (e.g., if 'spam' and 'spammy' are both prohibited).
- Case-sensitivity: 'Spam' should be treated the same as 'spam'.
Hints
Hint 1
Normalize the input string to lowercase and use a Set data structure to store prohibited words for O(1) average lookup time.
Hint 2
Use a Hash Map (Dictionary) to maintain the state of violation counts associated with each user_id.
Hint 3
Consider using regular expressions or word boundary matching to ensure you are matching whole words rather than substrings.
Follow-up questions
- How would you modify your design if the number of prohibited words grew to millions, making them too large to store in memory on a single server?
- If this service needs to process reviews asynchronously, how would you handle the violation count updates to ensure consistency in a distributed system?
- How would you implement an 'allow-list' that overrides the prohibited-words list for specific verified users?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.