Filter stop words out of a word stream while preserving order

Quick Overview

Remove every stop word from a stream of words while keeping the remaining words, duplicates included, in their original order. It tests hash set lookups, single-pass filtering, exact case-sensitive matching, and extending the filter to a stream that does not fit in memory.

Filter stop words out of a word stream while preserving order

Company: Perplexity

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are processing a stream of words in real time and need to filter out common stop words (such as `the`, `a`, `is`) before passing the words downstream. Given an array of strings `words` representing the incoming stream and an array of strings `stop_words`, return a new array containing every word from `words` that is not present in `stop_words`, in its original order. The filter is case-sensitive, and duplicate non-stop words stay in the output. ### Function Signature ```python def filter_stop_words(words: List[str], stop_words: List[str]) -> List[str]: ``` ### Rules - A word is removed only when it is exactly equal to one of the stop words. Matching is case-sensitive and compares whole words, never substrings. - Every occurrence of a stop word is removed, and every occurrence of any other word is kept. - The kept words appear in the same relative order as in `words`. - If every word is a stop word, return an empty list. ### Constraints - `1 <= len(words) <= 10^4` - `0 <= len(stop_words) <= 10^3` - `1 <= len(words[i]), len(stop_words[i]) <= 50` - All strings consist of lowercase English letters only. ### Examples ```text Input: words = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"], stop_words = ["the", "over"] Output: ["quick", "brown", "fox", "jumps", "lazy", "dog"] ``` `"the"` appears twice and `"over"` once, and all three occurrences are removed. The other six words keep their original order. ```text Input: words = ["a", "b", "c", "a", "b", "c"], stop_words = ["b"] Output: ["a", "c", "a", "c"] ``` Both occurrences of `"b"` are filtered out, while the repeated `"a"` and `"c"` are kept. ```text Input: words = ["is", "a", "is"], stop_words = ["a", "is"] Output: [] ``` Every word in the stream is a stop word, so nothing is left.

Overview: Remove every stop word from a stream of words while keeping the remaining words, duplicates included, in their original order. It tests hash set lookups, single-pass filtering, exact case-sensitive matching, and extending the filter to a stream that does not fit in memory.

Perplexity logo
Perplexity
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are processing a stream of words in real time and need to filter out common stop words (such as the, a, is) before passing the words downstream.

Given an array of strings words representing the incoming stream and an array of strings stop_words, return a new array containing every word from words that is not present in stop_words, in its original order.

The filter is case-sensitive, and duplicate non-stop words stay in the output.

Function Signature

def filter_stop_words(words: List[str], stop_words: List[str]) -> List[str]:

Rules

  • A word is removed only when it is exactly equal to one of the stop words. Matching is case-sensitive and compares whole words, never substrings.
  • Every occurrence of a stop word is removed, and every occurrence of any other word is kept.
  • The kept words appear in the same relative order as in words .
  • If every word is a stop word, return an empty list.

Constraints

  • 1 <= len(words) <= 10^4
  • 0 <= len(stop_words) <= 10^3
  • 1 <= len(words[i]), len(stop_words[i]) <= 50
  • All strings consist of lowercase English letters only.

Examples

Input: words = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"], stop_words = ["the", "over"]
Output: ["quick", "brown", "fox", "jumps", "lazy", "dog"]

"the" appears twice and "over" once, and all three occurrences are removed. The other six words keep their original order.

Input: words = ["a", "b", "c", "a", "b", "c"], stop_words = ["b"]
Output: ["a", "c", "a", "c"]

Both occurrences of "b" are filtered out, while the repeated "a" and "c" are kept.

Input: words = ["is", "a", "is"], stop_words = ["a", "is"]
Output: []

Every word in the stream is a stop word, so nothing is left.