Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Substring Search Scripting

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Substring Search Scripting. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Wolters Kluwer deployment tooling may need to detect a marker such as "ERROR" or "rollback" within a large log string. Implement a function that returns True when a pattern occurs in the text and False otherwise.

The match must be case-sensitive, and the pattern may occur at any position, including the beginning or end of the text. To support large logs efficiently, implement the Knuth-Morris-Pratt (KMP) algorithm rather than repeatedly comparing every possible substring from scratch.

Formal Specification

Implement contains_substring(text, pattern).

  • Input: text and pattern, both strings containing printable ASCII characters.
  • Output: A boolean. Return True if pattern is a contiguous substring of text; otherwise return False.
  • An empty pattern is considered present in every text, including an empty text.

Constraints

  • 0 <= len(text) <= 10^6
  • 0 <= len(pattern) <= 10^5
  • Both strings contain printable ASCII characters
  • Matching is case-sensitive
  • An empty pattern is considered present
  • The required time complexity is O(len(text) + len(pattern))

Function Signature

def contains_substring(text, pattern):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output