Prepin
Log in

Create a Simple Regex Parser

Approach / Explanation

This question requires you to create and execute a basic regex (Regular Expression) parser. Your primary objective is to establish a method for identifying specific patterns within provided string inputs and generating appropriate responses. Typically, this is evaluated based on the inclusion of the character "." which can represent any character, and the character "*" which can denote zero or more occurrences of the preceding elements. When addressing this question, keep the following points in mind: Attention to detail: Since this is a very specific question, the details matter greatly, including special cases and whether the input may contain an empty string, among other considerations. Logical progression: Begin with simple conditions and progressively introduce more complex ones to demonstrate your programming skills effectively. Testing: Once you've completed the implementation, test it against various cases to ensure that it adequately addresses all specified conditions.

Suggested Answer

Here’s a potential solution that utilizes a recursive method in Python: def isMatch(text, pattern): if not pattern: return not text first_match = bool(text) and pattern[0] in {text[0], '.'} if len(pattern) >= 2 and pattern[1] == '*': return (isMatch(text, pattern[2:]) or # * signifies zero occurrences of the preceding element first_match and isMatch(text[1:], pattern)) # * signifies one or more occurrences of the preceding element else: return first_match and isMatch(text[1:], pattern[1:]) # matches the first character This solution begins by checking if the pattern is empty. If it is, then if the text is also empty, it indicates a match, so it returns not text, which would evaluate to True if the text is empty. If the pattern is not empty, it compares the first character of both the pattern and the text. If they are the same or if the first character of the pattern is '.', it sets the first_match flag to True.

Alternative Answer

Here’s a potential solution utilizing a dynamic programming method in Python: def isMatch(text, pattern): dp = [[False] * (len(pattern) + 1) for _ in range(len(text) + 1)] dp[-1][-1] = True for i in range(len(text), -1, -1): for j in range(len(pattern) - 1, -1, -1): first_match = i This solution constructs a dynamic programming table to determine if pattern[j..] corresponds to text[i..]. If there is a match, dp[i][j] will be True. The process begins from the bottom-right corner of the table, evaluating all values and checking if the outcomes of smaller sub-problems can be utilized to address larger ones. The ultimate result is found in the top-left corner of the table.

Question Details

Difficulty & Category

Medium
algorithms

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.