AlgoMaster Logo

Maximum Nesting Depth of the Parentheses

easyFrequency4 min readUpdated June 23, 2026

Understanding the Problem

We're given a string containing digits, arithmetic operators, and parentheses, and we need to find how deeply the parentheses are nested. The nesting depth is the number of parentheses layers surrounding the most deeply nested part of the expression.

For (1+(2*3)+((8)/4))+1, look at the character 8. To reach it from the outside, you cross three opening parentheses: the outermost (, then the two (( just before 8. That gives a depth of 3, and no other character sits deeper, so the answer is 3.

Every ( increases the current depth by one and every ) decreases it by one. The answer is the largest depth reached at any point during a left-to-right scan.

Key Constraints:

  • s is a valid parentheses string → We don't have to handle mismatched or invalid parentheses. Every ) closes a previously opened (, so the running depth never goes negative. That guarantee is what lets us track depth with a single counter instead of validating structure as we go.
  • 1 <= s.length <= 100 → The bound is small, but a single left-to-right pass already solves this in O(n) time, so there is no reason to do anything heavier.

Approach 1: Using a Stack

Intuition

A stack tracks nesting directly: push on (, pop on ). At any moment the stack holds exactly the parentheses that are currently open, so its size equals the current nesting depth. The maximum nesting depth is the largest size the stack ever reaches.

Each ( opens a new level and each ) closes the innermost open level, which matches the push and pop operations one for one. Recording the peak stack size during the scan gives the deepest point in the expression.

Algorithm

  1. Create an empty stack.
  2. Iterate through each character in the string.
  3. If the character is (, push it onto the stack.
  4. If the character is ), pop from the stack.
  5. After each push, update the maximum depth if the stack size exceeds the current maximum.
  6. Return the maximum depth.

Example Walkthrough

1Start scanning. i=0: '(' → push, stack=1, maxDepth=1
0
(
i
1
1
2
+
3
(
4
2
5
*
6
3
7
)
8
+
9
(
10
(
11
8
12
)
13
/
14
4
15
)
16
)
17
+
18
1
1/6

Code

The stack never has its contents inspected. The only value read from it is its size, so the next approach replaces it with a single integer counter and drops the O(n) space.

Approach 2: Counter (Optimal)

Intuition

The stack from Approach 1 always holds copies of the same character, (, and the only thing read from it is its size. A single integer carries that same information. Increment it on (, decrement it on ), and the integer equals the current nesting depth at every step. The maximum value it reaches is the answer.

Algorithm

  1. Initialize depth = 0 and maxDepth = 0.
  2. Iterate through each character in the string.
  3. If the character is (, increment depth and update maxDepth if depth exceeds it.
  4. If the character is ), decrement depth.
  5. Return maxDepth.

Example Walkthrough

1Start: depth=0, maxDepth=0. i=0: '(' → depth=1, maxDepth=1
0
(
i
1
1
2
)
3
+
4
(
5
(
6
2
7
)
8
)
9
+
10
(
11
(
12
(
13
3
14
)
15
)
16
)
1/6

Code