Check Whether One String Contains Another by Direct Comparison
Quick Overview
Implement direct substring containment by checking each possible starting position. Handle empty patterns and boundary cases, and explain the repeated-prefix inputs that produce quadratic work.
Check Whether One String Contains Another by Direct Comparison
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Given a text string and a pattern string, determine whether the pattern occurs as a contiguous substring of the text. Use a direct brute-force comparison algorithm.
### Constraints and Clarifications
For this practice interface, return a Boolean, use exact case-sensitive character equality, and define the empty pattern as present in every text. The report specifies containment and brute force but no encoding or numerical bounds; treat strings as sequences of comparable characters without normalization.
### What a Strong Answer Covers
Try each valid start position, stop comparing a start at its first mismatch, and handle empty or longer patterns. Explain worst-case time and constant auxiliary space.
### Follow-up Questions
- What pattern of repeated characters makes this approach slow?
- What additional preprocessing could avoid repeated comparisons?
Overview: Implement direct substring containment by checking each possible starting position. Handle empty patterns and boundary cases, and explain the repeated-prefix inputs that produce quadratic work.
Given a text string and a pattern string, determine whether the pattern occurs as a contiguous substring of the text. Use a direct brute-force comparison algorithm.
Constraints and Clarifications
For this practice interface, return a Boolean, use exact case-sensitive character equality, and define the empty pattern as present in every text. The report specifies containment and brute force but no encoding or numerical bounds; treat strings as sequences of comparable characters without normalization.
What a Strong Answer Covers Guidance
Try each valid start position, stop comparing a start at its first mismatch, and handle empty or longer patterns. Explain worst-case time and constant auxiliary space.
Follow-up Questions Guidance
What pattern of repeated characters makes this approach slow?
What additional preprocessing could avoid repeated comparisons?