Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Regex Match Function

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

Your question is Regex Match Function. 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

Amplify Life Center QA checks may need to determine whether a text value contains a pattern. Implement a simplified regular-expression matcher that returns True when the pattern matches any contiguous substring of the text.

The pattern supports:

  1. Literal lowercase and uppercase letters, digits, spaces, and punctuation, which match themselves.
  2. . which matches any single character.
  3. * which matches zero or more occurrences of the immediately preceding literal or . token.

The entire pattern does not need to match the entire text. For example, cat matches a cat record.

Formal Specification

Implement contains_regex(text, pattern), where both inputs are strings. Return a boolean. The pattern is guaranteed to be valid, and * never appears without a preceding token.

Examples

Example 1

Input: text = "A member completed onboarding", pattern = "completed.*boarding"
Output: True

The pattern matches the substring completed onboarding.

Example 2

Input: text = "Amplify QA review", pattern = "q.a"
Output: True

The pattern matches Q followed by any character and A, case-sensitive matching permitting the exact substring QA only if the pattern is adjusted for the intervening character. Here, q.a does not match because uppercase and lowercase characters differ.

Constraints

  • 0 <= len(text) <= 500
  • 0 <= len(pattern) <= 100
  • The pattern is valid.
  • The pattern contains only literals, ., and *.
  • Matching is case-sensitive.
  • * applies only to the immediately preceding token.

Function Signature

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