FastPrepLongest Valid Parentheses
Problems/Longest Valid Parentheses

Longest Valid Parentheses

Apple logoApple● HardNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple new grad phone screen. The report names Longest Valid Parentheses. Exact examples and input limits were not shared. We added them. Our version is about 95% the same as the reported question.

Given a string s containing only ( and ), return the length of its longest contiguous valid parentheses substring.

A valid substring pairs every opening parenthesis with a later closing parenthesis, and no prefix of that substring has more closing parentheses than opening parentheses. Return 0 if no nonempty valid substring exists.

Function

longestValidParentheses(s: String) → int

Examples

Example 1

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

The substring at indices 1 through 4 is ()() and has length 4. The unmatched closing parentheses at the ends cannot extend it.

Example 2

s = "(()"return = 2

The last two characters form (). The first opening parenthesis remains unmatched, so the full string is invalid.

Constraints

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

More Apple Phone Screen questions

  • Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
  • Evaluate DivisionPHONE SCREEN · Seen Nov 2026
  • Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
  • Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
  • Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
  • Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
  • Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
  • Maintain a Consistent-Hashing RingPHONE SCREEN · Seen Oct 2026
  • Count Nodes in a ChainPHONE SCREEN · Seen Oct 2026
  • Run-Length DecodingPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Longest Properly Closed Bracket RunAdobe · PHONE SCREENAdobe interview questions
  • Longest Valid ParenthesesMathWorks · ONSITE INTERVIEWMathWorks interview questions
  • Longest Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Longest Valid ParenthesesMaven Clinic · ONSITE INTERVIEWMaven Clinic interview questions
  • Concatenated WordsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Parse Boolean Rule ExpressionsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Balanced Parentheses with Sequential Left DeletionsGoogle · PHONE SCREENGoogle interview questions
  • Word Break IIWex · PHONE SCREEN
  • Word Break IIInMobi · PHONE SCREENInMobi interview questions
  • Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public int longestValidParentheses(String s) {
        // Write your solution here.
    }
}
Checking account…