FastPrepLongest Valid Parentheses
Problems/Longest Valid Parentheses

Longest Valid Parentheses

Microsoft logoMicrosoft● HardINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A review of a Microsoft software engineering intern's technical interview mentions two algorithm questions. For the first, it names no problem and gives no input. Its only hints are that the answer is the greatest length of some part that is valid, without saying what valid means, and that a stack was used.

We read it as the classic longest valid parentheses task. The string has only ( and ), you return the length of the longest substring in which every parenthesis is matched, an empty string or no match gives 0, and the length limit of 3 * 10^4 is ours.

Our version is about 70% the same as the reported question.

Given a string s made only of ( and ), return the length of the longest substring that is a valid parentheses string.

A substring is a run of characters that are next to each other in s. A valid parentheses string is one where every ( is closed by a later ) and every ) closes an earlier (, for example (), (()) or ()(()). If no such non-empty substring exists, return 0.

Function

longestValidParentheses(s: String) → int

Examples

Example 1

s = ")()())"return = 4

The substring ()() (characters 2 to 5) is balanced and has length 4. The first ) has nothing to close, and adding the last ) would close nothing either, so no longer balanced substring exists.

Example 2

s = "(()"return = 2

Only () at the end is balanced. The first ( is never closed, so the answer is 2.

Example 3

s = ""return = 0

The empty string has no characters, so the answer is 0.

Constraints

  • 0 <= s.length <= 3 * 10^4
  • Every character of s is ( or ).

More Microsoft Onsite Interview questions

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
  • Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Longest Valid ParenthesesMathWorks · ONSITE INTERVIEWMathWorks interview questions
  • Longest Valid ParenthesesMaven Clinic · ONSITE INTERVIEWMaven Clinic interview questions
  • Longest Valid ParenthesesApple · PHONE SCREENApple interview questions
  • Longest Properly Closed Bracket RunAdobe · PHONE SCREENAdobe 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
  • Parse Boolean Rule ExpressionsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Interleaving StringsCARS24 · ONSITE INTERVIEW

Interview reports that mention this question

  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int longestValidParentheses(String s) {
        // Write your code here.
    }
}
Checking account…