Longest Valid Parentheses
Updated
Problem statement
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) → intExamples
Example 1
s = "(()"return = 2The longest valid contiguous substring is (), with length 2.
Example 2
s = ")()())"return = 4The longest valid contiguous substring is ()(), with length 4.
Example 3
s = ""return = 0The empty string contains no non-empty valid substring.
Constraints
0 <= s.length <= 3 * 10^4scontains only(and).
More Maven Clinic interview questions
- Paginate Provider Listings with Per-Page DiversityONSITE INTERVIEW · Seen Apr 2026
- Configurable Valid BracketsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2025
- Minimum Appointment Cancellations with a Required BreakPHONE SCREEN · Seen Jun 2026
- Shortest Currency Conversion with Three-Decimal RatePHONE SCREEN · Seen Mar 2026
- Meeting Rooms IIPHONE SCREEN · Seen Aug 2025
- Possible BipartitionPHONE SCREEN · Seen Aug 2024
Similar questions at other companies
- Longest Valid ParenthesesMathWorks · ONSITE INTERVIEWMathWorks interview questions
- Longest Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Longest Valid ParenthesesApple · PHONE SCREENApple interview questions
- Longest Properly Closed Bracket RunAdobe · PHONE SCREENAdobe interview questions
- Parse Boolean Rule ExpressionsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Minimum Insertion Steps to Make a String PalindromeInfosys · ONSITE INTERVIEWInfosys interview questions
- Wildcard MatchingTripjack · ONSITE INTERVIEWTripjack interview questions
- All Balanced Strings from Minimum Parenthesis InsertionsRubrik · ONSITE INTERVIEWRubrik interview questions
- Concatenated WordsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Interleaving StringsCARS24 · ONSITE INTERVIEW