Find the longest possible regular expression
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's regex matching problem hit the OA circuit in December 2024, and candidates are blanking on it. You're given a string and a pattern with '.' and '*' wildcards, and you need to determine if the string matches the pattern completely. This is a classic dynamic programming problem that rewards careful state setup. StealthCoder can catch you if the recursive logic slips during the live assessment.
The problem
The errors in the above example were corrected on 06-19-2025, thanks to the helpful source shared by an awesome friend! Here is the relevant ss for your reference -
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is building a 2D DP table where dp[i][j] represents whether the first i characters of the string match the first j characters of the pattern. The '*' wildcard is the wrinkle: it matches zero or more of the preceding character, so you need to handle both cases (use it, or skip it). Most candidates miss that you must check the character before '*' exists before deciding if a match is valid. The pattern is pure dynamic programming, not greedy. StealthCoder serves as your hedge if you freeze on the recurrence relation during the actual OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Find the longest possible regular expression 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as regular expression matching. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find the longest possible regular expression FAQ
How do I handle the '.' character?+
'.' matches any single character. When you see it in the pattern, treat it as a match for any character in the string at that position. Update your DP state accordingly without special branching logic.
What's the biggest mistake candidates make with '*'?+
Forgetting that '*' applies only to the character immediately before it, not the entire pattern. Also, not handling the zero-match case: you must allow '*' to match zero occurrences of its character by looking at dp[i][j-2].
Do I need memoization or a bottom-up table?+
Either works, but bottom-up DP is cleaner for this problem. Build the table left-to-right, top-to-bottom, so you always have the subproblem answers ready. Initialize dp[0][0] = true and handle empty patterns carefully.
How hard is the implementation really?+
The logic is 10-15 lines once you nail the state definition. The hardest part is the '*' case: check if the pattern at j-1 matches the string at i-1, then decide whether to use the wildcard or skip it by jumping to j-2.
Is this still being asked in December 2024 OAs?+
Yes. Amazon's regex matching problem is a perennial. It tests DP fundamentals and careful case handling. Expect it to stay in rotation for the foreseeable future.