PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Apple

Write a generator for substring pattern matches

Last updated: Mar 29, 2026

Quick Overview

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.

  • medium
  • Apple
  • Coding & Algorithms
  • Machine Learning Engineer

Write a generator for substring pattern matches

Company: Apple

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Write a generator that scans a string and emits a value whenever a substring matches a given pattern. ### Input - A string `s` - A string `pattern` (non-empty) ### Output Return a generator/iterator that yields `True` **once for each occurrence** of `pattern` in `s`, scanning from left to right. Matches may overlap. ### Examples - `s = "aaaa"`, `pattern = "aa"` → yields `True` 3 times (matches at indices 0, 1, 2) - `s = "abc"`, `pattern = "d"` → yields nothing ### Constraints - `0 <= len(s) <= 10^6` - `1 <= len(pattern) <= 10^5` ### Notes - The solution should avoid quadratic time in the worst case.

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.

Related Interview Questions

  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)
  • Encode and Rebuild a Binary Tree - Apple (hard)
  • Wrap Matching Substrings in Bold Tags - Apple (medium)
Apple logo
Apple
Dec 15, 2025, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
5
0

Problem

Write a generator that scans a string and emits a value whenever a substring matches a given pattern.

Input

  • A string s
  • A string pattern (non-empty)

Output

Return a generator/iterator that yields True once for each occurrence of pattern in s, scanning from left to right. Matches may overlap.

Examples

  • s = "aaaa" , pattern = "aa" → yields True 3 times (matches at indices 0, 1, 2)
  • s = "abc" , pattern = "d" → yields nothing

Constraints

  • 0 <= len(s) <= 10^6
  • 1 <= len(pattern) <= 10^5

Notes

  • The solution should avoid quadratic time in the worst case.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Apple•More Machine Learning Engineer•Apple Machine Learning Engineer•Apple Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.