Flag Banned Phrases as Characters Arrive in a Live Chat Stream
Last updated: November 29, 2026
Quick Overview
Given a list of banned phrases, build a checker whose `query(ch)` is called once per incoming character of a chat stream and returns True if any banned phrase ends exactly at that character.
Amazon
November 29, 20260
0
0 solved
Given a list of banned phrases, build a checker whose `query(ch)` is called once per incoming character of a chat stream and returns True if any banned phrase ends exactly at that character.
A harder screen question in Amazon's style, framed around moderating live chat on shopping livestreams where messages arrive character by character. The interviewer wants the reversed-trie insight, a bounded buffer, and a comparison with Aho-Corasick.
What the Interviewer Expects
- Notices that checking suffixes is easier with a trie of reversed phrases
- Keeps only the last max-length characters of the stream
- Stops scanning as soon as the trie walk fails
- Analyzes per-query cost as O(max phrase length)
- Mentions Aho-Corasick as the O(1) amortized alternative
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you make each query amortized O(1)?
- How would you handle case-insensitive and Unicode-normalized matching?
- How would you shard this across thousands of concurrent streams?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Phrases are known up front; up to around 2000 phrases of length up to 200, and up to queries per stream. query must return True iff some phrase equals a suffix of the characters seen ...
High-Level Design
A forward trie answers 'does a phrase start here', but we need 'does a phrase end here'. Reverse every phrase before inserting it, then on each query walk the trie from the newest character backward t...