32. Longest Valid Parentheses
Asked at Meta, Visa
Difficulty
Hard
Time
O(n)
Space
O(n)
Pattern
String
Companies
2
Problem
You are given a string s made only of the characters ( and ). Your job is to find the length of the longest valid parentheses substring.
A substring is a block of characters that sit next to each other in s. A substring is valid when every ( has a matching ) after it, and the pairs are properly nested. For example, "()" and "()()" are valid, but ")(" and "(()" are not.
Return a single integer: the length of the longest valid substring. If no valid substring exists, return 0.
Find the longest valid block
Constraints
- ●
0 <= s.length <= 3 * 10^4 - ●
s[i] is '(' or ')'
Examples
s = "(()"2s = ")()())"4s = ""0Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Meta | Hard | View all Meta questions → |
| Visa | Hard | View all Visa questions → |
How to Think About It
A brute force approach checks every possible substring and validates whether it is a well-formed parentheses string.
This is O(n^3) which is too slow for large inputs.
A stack-based approach can track indices of unmatched parentheses and compute valid lengths between them.
Use a stack initialized with -1 as a base index. Push index of open paren, pop on close paren and update max length.
A DP approach where dp[i] stores the length of the longest valid substring ending at index i can also work in O(n).
Optimal Approach
Use a stack initialized with -1 as a sentinel base index. Iterate through the string: push the index of each open parenthesis. For each close parenthesis, pop from the stack. If the stack is empty after popping, push the current index as the new base. Otherwise, update the maximum length as the difference between the current index and the new stack top. This computes the longest valid substring in O(n) time and O(n) space.
Stack of unmatched indices
What Trips People Up in Real Interviews
Clarify what constitutes a valid parentheses substring — each open must have a matching close in the correct order.
The stack approach is the most intuitive and commonly expected in interviews.
Walk through examples to show how the stack stores indices and computes lengths.
Discuss the two-pointer alternative which achieves O(1) space but is trickier to implement.
Edge cases: all open parens, all close parens, empty string, alternating invalid pattern like ")(".
Solution Code
O(n) timedef longestValidParentheses(s):
stack = [-1]
max_len = 0
for i in range(len(s)):
if s[i] == '(':
stack.append(i)
else:
stack.pop()
if not stack:
stack.append(i)
else:
max_len = max(max_len, i - stack[-1])
return max_lenFrequently Asked Questions
What is the Longest Valid Parentheses problem?
Given a string containing just the characters open and close parentheses, find the length of the longest valid (well-formed) parentheses substring.
How do you solve Longest Valid Parentheses?
The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Longest Valid Parentheses?
Longest Valid Parentheses is asked at Meta, Visa. It is a hard difficulty problem.
What are common mistakes on Longest Valid Parentheses?
- Clarify what constitutes a valid parentheses substring — each open must have a matching close in the correct order.
- The stack approach is the most intuitive and commonly expected in interviews.
- Walk through examples to show how the stack stores indices and computes lengths.
- Discuss the two-pointer alternative which achieves O(1) space but is trickier to implement.
- Edge cases: all open parens, all close parens, empty string, alternating invalid pattern like ")(".