Reported July 2026
MathWorksstack

Longest Valid Parentheses

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

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

The stack is what this one hinges on. MathWorks reportedly put Longest Valid Parentheses in front of candidates in July 2026, and it's a classic that punishes anyone who tries to wing it with a simple counter. You get a string of only ( and ), and you need the longest contiguous valid run. The hinted pattern is dynamic programming, but a stack of indices gets you there with less pain. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution as a hedge. Know the trick before you need it.

The problem

Given a string s containing only ( and ), return the length of the longest contiguous substring that forms valid parentheses.
A parentheses string is valid when every opening parenthesis is matched with a later closing parenthesis and no prefix contains more closing parentheses than opening parentheses.

Function
longestValidParentheses(s: String) → int

Examples
Example 1
s = "(()"
return = 2
The longest valid contiguous substring is (), with length 2.
Example 2
s = ")()())"
return = 4
The longest valid contiguous substring is ()(), with length 4.
Example 3
s = ""
return = 0
The empty string contains no non-empty valid substring.

Constraints
0 <= s.length <= 3 * 10^4
s contains only ( and ).

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to store indices, not characters. Seed the stack with -1 as a base marker. For each (, push its index. For each ), pop. If the stack is now empty, push the current index as the new base. Otherwise the current valid length is i minus the top of the stack. Track the max. That's O(n) time and O(n) space. The DP version uses dp[i] as the longest valid substring ending at i, and the transitions for "))" cases trip people up. The common pitfall is counting matched pairs and forgetting that a bad ) breaks contiguity, so ")()())" gives 4, not 6. Also handle the empty string and return 0. If the stack logic slips under pressure, StealthCoder is the safety net on the live OA, reading the problem and handing you working code without the proctor seeing it.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Longest Valid Parentheses 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as longest valid parentheses. If you have time before the OA, drill that.

⏵ The honest play

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

MathWorks reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Valid Parentheses FAQ

What's the trick for Longest Valid Parentheses?+

Use a stack of indices with -1 pushed first. Push on (, pop on ). If the stack empties, push the current index as a new base. Otherwise the length is i minus the stack top. Take the max across the string. It runs in one pass.

Is dynamic programming or a stack better here?+

Both work in O(n). The stack is easier to get right under time pressure because the logic is short and the base marker handles resets. The DP needs careful casework for when the previous character is ) and you must look back past a valid block.

How hard is this problem really?+

It's labeled hard, but the stack solution is about ten lines once you've seen it. The difficulty is the idea of storing indices and the base marker. Most failures come from forgetting the reset when the stack empties.

What edge cases should I test?+

Test the empty string, which returns 0. Test a string of only ) characters, only ( characters, and ")()())" which should give 4. Also try "(()" for 2 and nested cases like "((()))" to confirm the length math.

How do I prepare in 48 hours?+

Write the stack solution from scratch twice without looking. Then trace ")()())" by hand, tracking the stack at each step. Do the two-pass counter variant once too, since it's O(1) space. That's enough for this pattern.

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

OA at MathWorks?
Invisible during screen share
Get it