Detect disguised abusive words in comments
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon. It tests trie, string matching and nfa.
A comment platform wants to catch abusive words that users disguise with look-alike characters, such as $ for s or 0 for o.
Implement AbuseDetector:
-
AbuseDetector(banned, substitutions):bannedis a list of lowercase words.substitutionsmaps a disguise character to the letters it can stand for, as a string. One character can stand for several letters, for example"1": "il". -
detect(comment) -> List[str]: returns the banned words found in the comment, in order of appearance. -
flag(comments) -> List[int]: returns the indices of comments that contain at least one banned word.
Rules
- Matching is case-insensitive.
- A token is a maximal run of characters that are letters, digits, or disguise characters. Every other character separates tokens.
- A token matches a banned word when the whole token can spell it, reading each character either as itself (if it is a letter) or as one of the letters it can stand for. A banned word inside a longer token does not count.
Constraints
- Up to 10^5 banned words with total length up to 10^6.
- Comments up to 10^4 characters. A character can stand for up to 3 letters.
Example
banned = ["jerk", "idiot", "loser"]
substitutions = {"$": "s", "0": "o", "3": "e", "!": "i", "1": "il", "|": "il"}
detect("What an !d!0t, honestly. J3RK.") -> ["idiot", "jerk"]
detect("|0$3r") -> ["loser"]
detect("jerky snacks") -> []
flag(["great post", "j3rk", "l0ser!"]) -> [1]
In the last call, ! is a disguise character, so l0ser! is one token. It spells loseri, which is not banned.
Part 2. Users also stretch words, as in jeeeerk or l0000ser. Add an option where each letter of a banned word may appear one or more times in a row. Interviewers may then ask about separators inside words (j.e.r.k), banned words inside longer tokens, and multi-character disguises such as |< for k.
Clarifying questions
- Can a character that is a letter also be a disguise (for example
lstanding fori)? - If one token can spell two banned words, are both reported, or one?
- Is a banned word found twice in one comment reported twice?
- Are digits that are not disguise characters part of tokens (the rules say yes): confirm
jerk2is one token.
What a strong answer covers
- Tokenises in one pass with the rule given (letters, digits, disguise characters), lowercasing.
- Stores banned words in a trie (or a dict by length), then matches each token by walking the trie with a set of active nodes, branching for each letter a character can stand for: at most 3 branches per character.
- Explains why trying every expansion of the token is exponential and why the trie walk bounds the work by trie size and token length.
- Handles whole-token matching only (
jerkydoes not match), mixed case, and a trailing disguise character that changes the word (l0ser!). - Tests multi-meaning characters (
1asiorl), uppercase disguises, and comments with no tokens. - For Part 2, extends the trie walk so a letter may repeat (stay on a node while the same letter repeats).
Follow-ups
- Ignore separators inside words, like
j.e.r.k. - Find banned words inside longer tokens without flagging innocent words (the classic false-positive problem).
- Support multi-character disguises such as
|<fork.
Where it's asked
What it tests
trie · string matching · nfa
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
