Magicsheet logo

Stream of Characters

Hard
65.5%
Updated 6/1/2025

Stream of Characters

What is this problem about?

The Stream of Characters coding problem involves designing a system that can efficiently check if any suffix of a growing stream of characters matches a predefined set of words. You are given a dictionary of words, and then a series of characters are queried one by one. After each new character is added to the stream, you must return true if any word from the dictionary ends at that character, and false otherwise. This is a classic "real-time" processing problem.

Why is this asked in interviews?

This is a high-frequency Stream of Characters interview question at top firms like Jane Street, Salesforce, and Google. It tests a candidate's ability to design a data structure that handles frequent updates and queries. The challenge lies in the efficiency; a naive search after every character would be far too slow. Interviewers are looking for your knowledge of Tries (Prefix Trees) and your ability to adapt them—specifically, searching for words in reverse to match the "suffix" requirement of the stream.

Algorithmic pattern used

The problem fits the Data Stream, Array, Design, Trie, String interview pattern. The most efficient way to solve this is to insert all dictionary words into a Trie in reverse order. You also maintain a buffer of the last N characters received in the stream (where N is the length of the longest word). When a query comes, you traverse the Trie starting from the most recent character in the stream and moving backwards. If you hit a node marked as the "end" of a word during this traversal, you return true.

Example explanation

Dictionary: ["cat", "bat", "rat"] Trie (reversed):

  • t -> a -> c (end)
  • t -> a -> b (end)
  • t -> a -> r (end)

Stream queries:

  1. 'c': Stream="c". Reverse="c". No match in Trie.
  2. 'a': Stream="ca". Reverse="ac". No match.
  3. 't': Stream="cat". Reverse="tac". In the Trie, we follow 't' -> 'a' -> 'c' and find the "end" marker. Return true.
  4. 'z': Stream="catz". Reverse="ztac". 'z' isn't in the root of our Trie. Return false.

Common mistakes candidates make

A common mistake is storing words in the Trie in their normal order. This makes the query very difficult because you don't know where the word might start in the stream. Another mistake is not limiting the character buffer, which can lead to unnecessary memory usage or slow lookups if the stream is very long. Candidates also sometimes forget to mark the end of words in the Trie or fail to handle the case where one dictionary word is a suffix of another.

Interview preparation tip

Tries are essential for any problem involving a dictionary or a large set of strings. Practice implementing a basic Trie from scratch, including insert and search methods. For "stream" problems, always consider the direction of the query. If you're matching suffixes, reversing the input is a powerful trick. Also, consider the space-time tradeoff: the Trie uses more memory but provides much faster query times compared to string matching algorithms like KMP in this specific context.

Similar Questions