Detect comments that contain inappropriate words using a trie

Quick Overview

Given a list of inappropriate words and a list of comments, decide for each comment whether it contains any of the words as a case-insensitive substring. The problem tests building a trie, scanning text from every start position, and reasoning about the cost of searching for many patterns at once.

Detect comments that contain inappropriate words using a trie

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a list of inappropriate words and a list of comments, determine whether each comment contains any of the inappropriate words as a substring. Use a trie to search for the inappropriate words within each comment efficiently. The matching is case-insensitive. Return a list of booleans where each element tells whether the corresponding comment contains at least one inappropriate word. ### Function Signature ```python def detect_inappropriate(inappropriate_words: list[str], comments: list[str]) -> list[bool]: ... ``` ### Rules - A word matches when it appears anywhere in the comment as a contiguous run of characters, including inside a longer word: "bad" matches "badge". - Spaces and punctuation are ordinary characters. Comments are not split into tokens. - Case is ignored on both sides, so the word "Bad" matches "BAD", "bad" and "bAd" in a comment. - The output has one boolean per comment, in the same order as `comments`. ### Constraints - `1 <= len(inappropriate_words) <= 1000` - `1 <= len(inappropriate_words[i]) <= 100` - `1 <= len(comments) <= 1000` - `1 <= len(comments[i]) <= 10^4` - All words and comments consist of printable ASCII characters. ### Examples ```text Input: inappropriate_words = ["bad", "hate", "ugly"], comments = ["This is a bad comment", "This is a good comment", "I hate this", "You are ugly"] Output: [True, False, True, True] ``` "bad" appears in comment 0, "hate" in comment 2 and "ugly" in comment 3. Comment 1 contains none of the three words. ```text Input: inappropriate_words = ["spam", "scam"], comments = ["Hello world", "This is a scam", "Nice day", "Buy spam now"] Output: [False, True, False, True] ``` "scam" appears in comment 1 and "spam" in comment 3. Comments 0 and 2 contain neither word. ```text Input: inappropriate_words = ["Bad"], comments = ["BADGE holder", "Nothing here", "b a d"] Output: [True, False, False] ``` "BADGE holder" lowercases to "badge holder", which contains "bad". In "b a d" the letters are separated by spaces, so "bad" is not a contiguous substring.

Overview: Given a list of inappropriate words and a list of comments, decide for each comment whether it contains any of the words as a case-insensitive substring. The problem tests building a trie, scanning text from every start position, and reasoning about the cost of searching for many patterns at once.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a list of inappropriate words and a list of comments, determine whether each comment contains any of the inappropriate words as a substring.

Use a trie to search for the inappropriate words within each comment efficiently. The matching is case-insensitive.

Return a list of booleans where each element tells whether the corresponding comment contains at least one inappropriate word.

Function Signature

def detect_inappropriate(inappropriate_words: list[str], comments: list[str]) -> list[bool]:
    ...

Rules

  • A word matches when it appears anywhere in the comment as a contiguous run of characters, including inside a longer word: "bad" matches "badge".
  • Spaces and punctuation are ordinary characters. Comments are not split into tokens.
  • Case is ignored on both sides, so the word "Bad" matches "BAD", "bad" and "bAd" in a comment.
  • The output has one boolean per comment, in the same order as comments .

Constraints

  • 1 <= len(inappropriate_words) <= 1000
  • 1 <= len(inappropriate_words[i]) <= 100
  • 1 <= len(comments) <= 1000
  • 1 <= len(comments[i]) <= 10^4
  • All words and comments consist of printable ASCII characters.

Examples

Input: inappropriate_words = ["bad", "hate", "ugly"], comments = ["This is a bad comment", "This is a good comment", "I hate this", "You are ugly"]
Output: [True, False, True, True]

"bad" appears in comment 0, "hate" in comment 2 and "ugly" in comment 3. Comment 1 contains none of the three words.

Input: inappropriate_words = ["spam", "scam"], comments = ["Hello world", "This is a scam", "Nice day", "Buy spam now"]
Output: [False, True, False, True]

"scam" appears in comment 1 and "spam" in comment 3. Comments 0 and 2 contain neither word.

Input: inappropriate_words = ["Bad"], comments = ["BADGE holder", "Nothing here", "b a d"]
Output: [True, False, False]

"BADGE holder" lowercases to "badge holder", which contains "bad". In "b a d" the letters are separated by spaces, so "bad" is not a contiguous substring.