Find Pattern in Infinite Stream II - Problem
Pattern Matching in an Infinite Bit Stream

You are given a binary array pattern and an InfiniteStream object that represents an infinite stream of bits (0s and 1s). The stream provides a single method:

int next(): Reads and returns the next bit from the stream

Goal: Find the first starting index where the given pattern matches the bits read from the stream.

For example, if pattern = [1, 0] and the stream contains [0, 1, 0, 1, 0, ...], the first match occurs at index 1 (where we see the subsequence [1, 0]).

Challenge: Since the stream is infinite and we can only read bits sequentially, we need an efficient algorithm that doesn't require storing the entire stream in memory.

Input & Output

example_1.py โ€” Basic Pattern Match
$ Input: pattern = [1, 0], stream = InfiniteStream([0, 1, 0, 1, 0, ...])
โ€บ Output: 1
๐Ÿ’ก Note: The pattern [1, 0] first appears starting at index 1 in the stream [0, 1, 0, 1, 0, ...]
example_2.py โ€” Pattern at Beginning
$ Input: pattern = [1, 1, 0], stream = InfiniteStream([1, 1, 0, 0, 1, 1, 0, ...])
โ€บ Output: 0
๐Ÿ’ก Note: The pattern [1, 1, 0] appears immediately at the start of the stream
example_3.py โ€” Single Bit Pattern
$ Input: pattern = [1], stream = InfiniteStream([0, 0, 1, 0, 1, ...])
โ€บ Output: 2
๐Ÿ’ก Note: The first occurrence of bit '1' is at index 2 in the stream

Visualization

Tap to expand
Infinite Stream Pattern Matching0101...pos 0pos 1pos 2pos 3Sliding WindowPattern: [1, 0]Hash: 2ร—1 + 0 = 2Rolling Hash UpdateO(1) time complexityAlgorithm Steps1. Calculate pattern hash2. Initialize sliding window3. Compare window hash vs pattern4. If match: verify actual bits5. Slide window: update hash O(1)6. Return position when foundTime: O(n + m) | Space: O(m)Optimal for infinite streams - memory efficient!
Understanding the Visualization
1
Setup Pattern Hash
Calculate hash value for the target pattern [1,0]
2
Initialize Window
Read first pattern.length bits from stream into sliding window
3
Rolling Hash Check
Compare current window hash with pattern hash
4
Slide Window
Remove oldest bit, add new bit, update hash in O(1)
5
Pattern Found
When hashes match and bits verify, return the starting position
Key Takeaway
๐ŸŽฏ Key Insight: Rolling hash allows us to efficiently search infinite streams by updating window hash in O(1) time, avoiding the need to store the entire stream while maintaining optimal performance.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Where n is the number of bits read until match found and m is pattern length. Each bit requires O(1) hash update.

n
2n
โœ“ Linear Growth
Space Complexity
O(m)

Only store the current window of m bits (pattern length)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค pattern.length โ‰ค 104
  • pattern[i] is either 0 or 1
  • stream.next() returns either 0 or 1
  • The stream is guaranteed to contain the pattern
  • Memory constraint: Avoid storing the entire infinite stream
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
43.2K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen