← Microsoft Interview Insights
The 'split across chunks' constraint is what makes this non-trivial.
Use a rolling buffer that holds only the last (stopTokenLength - 1) characters, since any match must complete within that window. Process each chunk by appending to the buffer, scanning for the stop token, and emitting all characters before the match (or all but the last stopTokenLength-1 characters if no match). This ensures O(n) time and O(m) space, where m is the stop token length.
Pro tip: Mention that you can optimize the search using the KMP algorithm or a rolling hash to avoid re-scanning the buffer, but for typical stop token lengths, a simple scan is sufficient. Also, clarify how you handle the stop token at the very end of the stream (e.g., if the stream ends without a full match, you must flush the remaining buffer).
Ask about the stop token length, whether it can be empty, and if the stop token itself should be excluded from output. Confirm that chunks arrive sequentially and that you cannot store the entire stream.
Maintain a buffer of size at most (stopTokenLength - 1) characters. This is the maximum overlap needed to detect a stop token split across chunks.
Append the chunk to the buffer, then search for the stop token. If found, output all characters before the match and stop. If not found, output all characters except the last (stopTokenLength - 1) characters, and keep those in the buffer.
If the stream ends without finding the stop token, flush the remaining buffer to output. If the stop token is found, ensure no further chunks are processed.
Discuss time complexity O(n) and space O(m). Cover edge cases: stop token longer than chunk, stop token at chunk boundary, empty stop token, and stop token not present.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.