FastPrepMaximum Nesting Depth of Parentheses

Maximum Nesting Depth of Parentheses

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Given a string expression whose parentheses are balanced, return the maximum number of simultaneously open parentheses at any position.

Characters other than ( and ) do not affect the depth.

Function

maxDepth(expression: String) → int

Examples

Example 1

expression = "(1+(2*3)+((8)/4))+1"return = 3

The innermost parentheses around 8 are nested inside two other open pairs.

Example 2

expression = "(1)+((2))+(((3)))"return = 3

The final term opens three parenthesis pairs before closing any of them.

Example 3

expression = "1+2"return = 0

The expression contains no parentheses.

Constraints

  • 1 <= expression.length <= 100000
  • expression contains printable ASCII characters.
  • The parentheses in expression are balanced.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int maxDepth(String expression) {
    // Write your code here.
}
expression"(1+(2*3)+((8)/4))+1"
expected3
Checking account…