FastPrepLongest Valid Parentheses
Problems/Longest Valid Parentheses

Longest Valid Parentheses

Maven Clinic logoMaven Clinic● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Maven Clinic MLE onsite report says the last bracket follow-up was the common hard problem about the longest valid match. Our version uses the usual definition of Longest Valid Parentheses. It is about 98% the same as the reported question.

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 ).

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
All Maven Clinic interview questionsSee Maven Clinic hiring insights
Saved
class Solution {
    public int longestValidParentheses(String s) {
        // Write your code here.
    }
}
Checking account…