HARDasked at 21 companies

Longest Valid Parentheses

A hard-tier problem at 36% community acceptance, tagged with String, Dynamic Programming, Stack. Reported in interviews at SOTI and 20 others.

Founder's read

Longest Valid Parentheses is a hard string problem that shows up across 21 companies including Amazon, Microsoft, and eBay. The acceptance rate sits around 36%, meaning most candidates who see it live either freeze or code a slow brute force that times out. You're given a string of parentheses and need to find the length of the longest valid substring. The trap is that a greedy or naive approach won't work, and the pattern isn't obvious until you've seen it or have real-time help. If this problem hits your assessment and you blank on the dynamic programming or stack trick, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
21
Difficulty
HARD
Acceptance
36%

Companies that ask "Longest Valid Parentheses"

If this hits your live OA

Longest Valid Parentheses is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.

Get StealthCoder
What this means

The naive approach counts matching pairs or scans left-to-right, but neither catches all valid substrings because validity is about contiguous matching sequences, not just counts. The real solution uses either a stack to track unmatched indices or a DP array where each position stores the length of the longest valid substring ending at that index. Most candidates know these topics exist but can't recall which one applies or make off-by-one errors during implementation. The stack approach requires you to push indices, not characters, and handle the base case of an empty match stack carefully. Dynamic programming is cleaner conceptually but has subtle edge cases around how you propagate lengths from previous valid substrings. StealthCoder is your safety net if the pressure of the live assessment wipes out your recall on either pattern.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Longest Valid Parentheses recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Valid Parentheses interview FAQ

Is this really asked that often at Amazon and Microsoft?+

Yes. It appears in roughly 36% of reported interview rounds at these companies and crosses at least 10 major tech names, including eBay, Intuit, and Sprinklr. It's stable in rotation. If you're interviewing at a FAANG or mid-market shop, expect it as a possibility.

What's the actual trick, or is it just brute force?+

Brute force times out. The trick is either a stack that tracks unmatched indices or dynamic programming where dp[i] stores the longest valid substring ending at position i. Most misses come from not recognizing the DP recurrence or managing the stack base case correctly.

Can I solve this with greedy left-right scanning?+

No. Greedy fails because a valid substring can be broken by an unmatched opening parenthesis in the middle. For example, '(()())' requires you to recognize that the entire string is valid, not just portions of it. You need state tracking via stack or DP.

How does this relate to the other parentheses problems?+

Valid Parentheses is easy (36% vs ~90% acceptance); this one adds the substring constraint. You can't just check if the whole string is valid. You also need to find the longest contiguous chunk of valid parentheses, which requires either linear scan with indices or DP state transitions.

If I see this live and freeze, what's my move?+

State that you know the problem requires either a stack or DP, then write a clean brute force to show you understand validity checking. That buys time. If the interviewer pushes for optimal, StealthCoder runs invisibly during screen share and shows you the DP or stack solution in seconds, keeping you in the round.

Want the actual problem statement? View "Longest Valid Parentheses" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.