Reported June 2024
Amazondynamic programming

Is Regex Matching

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon asked this in June 2024, and you're probably looking at it right now. Is Regex Matching is a classic dynamic programming trap: it feels like you should build a regex engine, but the real problem is about filling a 2D table where rows are characters of the string and columns are characters of the pattern. The twist is handling the wildcard asterisk, which can match zero or more of the preceding character. If you blank on the recurrence, StealthCoder will pull the pattern and walk you through the base cases and transitions in real time.

Pattern and pitfall

This is a pure DP problem disguised as a string-matching problem. You build a table where dp[i][j] represents whether the first i characters of the string match the first j characters of the pattern. Base case: empty string matches empty pattern (and patterns with asterisks that cancel characters). The recurrence splits on three cases: literal character match, dot wildcard (matches any single char), and asterisk (match zero of the preceding char, or one-or-more by advancing the string pointer). The catch: asterisk depends on the character before it in the pattern, so you can't handle it in isolation. Most candidates miss the zero-match branch or mess up the index arithmetic. StealthCoder acts as your safety net if the DP formulation escapes you mid-OA.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Is Regex Matching cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as regular expression matching. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Is Regex Matching FAQ

Is this actually about implementing regex?+

No. You're not building a regex engine. You're solving a specific matching problem with literal characters, dot, and asterisk only. The structure is pure DP: fill a 2D table where rows are string indices and columns are pattern indices. Forget regex libraries.

What's the hardest part candidates get wrong?+

Handling asterisk. People forget that asterisk matches zero occurrences, which means dp[i][j] depends on dp[i][j-2], not just advancing the string. Also, asterisk can't be the first character in the pattern, and it always refers to the character before it.

What's the trick for the zero-match case?+

If the pattern has an asterisk at position j, dp[i][j] is true if dp[i][j-2] is true. This means the asterisk and its preceding character are skipped entirely, so zero characters are matched. This often trips people because it's not intuitive from a string-matching angle.

Can I solve this without DP?+

Not cleanly. Greedy fails because you don't know whether to consume characters with asterisk until you've tried both paths. Backtracking works but is exponential. DP with O(m*n) space and time is standard. If you've got 20 minutes left, don't reinvent.

How do I prepare for this in 48 hours?+

Draw the DP table by hand for a small example like s='aa', p='a*'. Trace the recurrence. Understand why dp[2][2] depends on dp[2][0]. Code it once cleanly. Amazon cares about the DP structure, not optimizations. One solid solution beats half-finished attempts.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it