The Find Pattern in Infinite Stream II interview question is the "Hard" version of the stream matching task. The pattern can be much longer ( bits), making a simple integer-based rolling hash insufficient due to bit-depth limits. You need a robust string-matching algorithm that can process bits one by one as they arrive from an infinite source.
Uber uses this to evaluate a candidate's knowledge of advanced string matching algorithms like KMP (Knuth-Morris-Pratt). KMP is uniquely suited for streams because it uses a "failure function" (partial match table) to determine the next state based only on the current character and the current state, without ever needing to "look back" at previous bits. It tests high-level String Matching interview patterns.
This problem is solved using the KMP Algorithm (State Machine).
LPS (Longest Proper Prefix which is also a Suffix) array for the target pattern.patternPointer initialized to 0.stream.next():
pattern[patternPointer], increment the pointer.LPS array to "jump" the pointer back to the next best possible prefix match.patternPointer reaches the end of the pattern, return the current stream index.Pattern: 1011
pointer is at 3.pattern[3] is 1. Mismatch!pointer to index 1 (after the first '1') and check the new bit '0' against pattern[1].pattern[1] is 0. Match! pointer moves to 2.
This avoids re-reading the stream and keeps the processing per bit.next() once per bit; you cannot "rewind" the stream.KMP is one of the most feared string algorithms. If you can explain how the "Partial Match Table" allows the search to continue without backtracking, you show a deep understanding of state machines and algorithm efficiency. This is a top-tier String interview pattern.