Design a chat profanity filter with whole-word, case-insensitive censoring and censor counts
Company: Riotgames
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a chat profanity filter that keeps a dynamic set of banned words and censors them in user messages.
Matching is case-insensitive and applies only to whole words: a banned word inside a larger word is left alone. A word is a maximal run of ASCII letters and digits, so every other character is a boundary. When a banned word is found, every character of that word in the original message is replaced with `*`, so the censored message has the same length as the original.
The filter also tracks how many times each banned word has been censored across all `filter` calls, so moderators can review repeat offenses.
Implement these methods:
- `addWord(word: str) -> None` adds `word` to the banned set, case-insensitively. It does nothing if the word is already present.
- `removeWord(word: str) -> bool` removes `word` from the banned set. It returns `True` if the word was present and removed, otherwise `False`.
- `filter(message: str) -> str` returns the message with every whole-word occurrence of any banned word replaced by `*` characters of equal length, and updates the censor counts.
- `getCensorCount(word: str) -> int` returns the total number of times `word` (case-insensitive) has been censored across all earlier `filter` calls, or `0` if it never was.
Example 1 (case and whole words):
```text
f = ChatProfanityFilter()
f.addWord("darn")
f.addWord("HECK") # stored case-insensitively
f.filter("Darn it, what the heck!") # "**** it, what the ****!"
f.filter("darnit, DARN. Heckler") # "darnit, ****. Heckler"
f.getCensorCount("DARN") # 2
f.getCensorCount("heck") # 1
```
`darnit` and `Heckler` contain banned words but are longer words, so they stay. `darn` was censored once in each message, `heck` once in total.
Example 2 (boundaries, removal and history):
```text
f = ChatProfanityFilter()
f.addWord("noob")
f.addWord("noob") # already present, no change
f.filter("noob_noob noob123 n00b") # "****_**** noob123 n00b"
f.removeWord("NOOB") # True
f.removeWord("noob") # False
f.filter("noob") # "noob"
f.getCensorCount("noob") # 2
```
The underscore is not a letter or digit, so `noob_noob` holds two whole words. `noob123` is a single word that is not banned, and `n00b` is a different word. After removal the word is no longer censored, and its count still reports the two earlier censors.
```hint Pin down what a word is
Decide exactly where a word starts and ends before writing any matching code, and test the rule on text where a banned word touches an underscore, a digit or punctuation.
```
```hint Which side to loop over
The banned list can grow large while each message is short. Think about which collection the loop should run over and which one should answer a hash lookup.
```
### Constraints & Assumptions
- `1 <= word.length <= 50`
- `1 <= message.length <= 10^4`
- Words and messages consist of printable ASCII characters.
- Matching is case-insensitive and only matches whole words; boundaries are non-alphanumeric characters.
- At most `10^4` calls in total across all methods.
### Clarifying Questions to Ask
- Can a banned entry contain spaces or punctuation, such as a two-word phrase, and if so how should it match?
- After `removeWord`, should `getCensorCount` still report the word's past censors, and does re-adding the word continue from that count?
- Should disguised spellings such as `n00b` or `n o o b` be caught, or only exact words?
- Can `filter` run concurrently with `addWord` and `removeWord`?
### What a Strong Answer Covers
- A precise definition of a word and how it treats underscores, digits and punctuation next to a banned word.
- Case-insensitive lookup through a normalized key, while the output keeps uncensored text exactly as typed and censored words at their original length.
- One linear pass over the message with a hash lookup per word, and the cost of each method.
- Counting semantics: per occurrence, keyed case-insensitively, kept across removals.
- The limits of exact whole-word matching (phrases, disguised spellings) and how the design could grow to cover them.
### Follow-up Questions
- How would you support banned phrases of several words?
- Users dodge filters with spellings like `n00b`, `n.o.o.b` or `noooob`. How would you catch those without censoring innocent words?
- Chat servers call `filter` from many threads while moderators edit the list. How would you keep the class correct and fast under that load?
- Moderators want counts per user as well as per word. What changes?
Overview: Design a chat profanity filter class that adds and removes banned words, censors whole-word matches case-insensitively with asterisks of equal length, and counts censored occurrences per word. It tests word-boundary rules, tokenization, hash-based lookup and clean state handling across calls.