FastPrepLongest Properly Closed Bracket Run
Problems/Longest Properly Closed Bracket Run

Longest Properly Closed Bracket Run

Adobe logoAdobe● HardFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem returns the length of the longest stretch of a bracket string that opens and closes properly. In an Adobe CS 1 report it is one of two first-round questions, given by a link; only the approach was talked through, and no code was asked for.

Our choices: the string holds only ( and ), it may be empty, it has at most 3000 characters, and the examples are new.

The judged task matches the report at about 90%.

A string of brackets is valid when every ( is closed by a later ) and every ) closes an earlier (, like (()()).

Given a string s of ( and ), return the length of its longest contiguous substring that is valid (0 if there is none).

Function

longestValid(s: String) → int

Examples

Example 1

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

The first two characters () are valid, but the third character ) has no partner and breaks the run. After it, (()) is valid and has length 4.

Example 2

s = "(((("return = 0

No ( is ever closed, so there is no valid stretch: 0.

Example 3

s = "()(()"return = 2

() has length 2. The rest, ((), contains the valid () at the end, also length 2, but the unclosed ( in the middle stops the two from joining. The answer is 2.

Constraints

  • 0 <= s.length <= 3000
  • s contains only the characters '(' and ')'

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int longestValid(String s) {
    // Write your code here.
}
Checking account…