Write a generator for substring pattern matches
Company: Apple
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of string pattern matching and generator/iterator design, testing skills in handling overlapping matches, streaming output, and worst-case time complexity considerations.
Constraints
- 0 <= len(s) <= 10^6
- 1 <= len(pattern) <= 10^5
- Matches may overlap
- The solution should run in O(len(s) + len(pattern)) time
Examples
Input: ("aaaa", "aa")
Expected Output: [True, True, True]
Explanation: The pattern "aa" occurs at starting indices 0, 1, and 2, including overlapping matches.
Input: ("abcdef", "gh")
Expected Output: []
Explanation: The pattern never appears in the string.
Input: ("abababa", "aba")
Expected Output: [True, True, True]
Explanation: The pattern "aba" occurs at starting indices 0, 2, and 4.
Input: ("short", "longpattern")
Expected Output: []
Explanation: A pattern longer than the text cannot match.
Input: ("", "a")
Expected Output: []
Explanation: An empty text contains no non-empty pattern occurrences.
Input: ("abc", "")
Expected Output: []
Explanation: By definition for this problem, an empty pattern produces no yielded values.
Hints
- A naive check at every starting index can become quadratic when the string and pattern contain many repeated characters.
- Consider preprocessing the pattern so that after a mismatch, you know how much of the current partial match can still be reused.