Reported August 2025
Maven Clinicstack

Longest Valid Parentheses

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

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

Maven Clinic reported this one in August 2025, and it's Longest Valid Parentheses wearing no disguise. Strip the wording and it reduces to one question: where does a matched run start, and how far does it stretch before something breaks it? If you've got an OA invite and 48 hours, that's the whole game. You can solve it with a stack of indices or a DP array, and both run in linear time. The trap is the edge cases, not the idea. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution in real time as a safety net.

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 cleanest approach is a stack of indices. Push -1 first as a base marker. For each character, push the index on '('. On ')', pop. If the stack is now empty, push the current index as the new base. Otherwise the current valid length is i minus the stack top. Track the max. That base marker is the whole trick, because it lets you measure a run that starts right after the last unmatched ')'. The common pitfall is counting matched pairs instead of contiguous length, which fails on inputs like "()(()". Another is forgetting the empty string, which should return 0. The DP version, where dp[i] is the longest valid run ending at i, works too but has fiddly index math for the case of ")" following ")". Pick the stack. If the live OA throws you off and you can't recall the base-index move, StealthCoder is the hedge that gets you unstuck without anyone seeing it.

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 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. 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 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 Maven Clinic's OA.

Maven Clinic 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.

Longest Valid Parentheses FAQ

What's the actual trick in Longest Valid Parentheses?+

Use a stack of indices seeded with -1. Push indices for '(', pop on ')'. If the stack empties, push the current index as a new base. Otherwise length is i minus the top of the stack. One pass, O(n) time, and the base marker handles every reset.

Is the stack or DP approach better for the Maven Clinic OA?+

Stack. It's shorter and has fewer off-by-one traps. DP needs separate cases for "()" endings and "))" endings, and the index math is easy to get wrong under pressure. Both are O(n), so correctness and speed of writing should decide it.

How hard is this problem really?+

It's labeled hard, but the solution is about ten lines once you know the base-index idea. The difficulty is seeing it cold. If you've seen the stack pattern once, you can write it from memory. Edge cases are the empty string and strings with no valid pair.

What edge cases should I test before submitting?+

Test the empty string (0), "(((" (0), ")))" (0), "()(()" (2), ")()())" (4), and "(()" (2). The last few catch the common bug of counting pairs instead of contiguous length, and the reset after an unmatched closing parenthesis.

How do I prepare for this in 48 hours?+

Write the stack solution from scratch twice without looking. Then trace ")()())" by hand and watch the stack change. Do one run of the DP version if you have time. Aim to explain why -1 is pushed first, since that's the part people forget.

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

OA at Maven Clinic?
Invisible during screen share
Get it