Maximum Nesting Depth of the Parentheses
Counter as Implicit Stack
Asked at 8 companies
4 recent
- Rippling
- Meta
- Amazon
- Intel
- Bloomberg
- tcs
- Microsoft
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a valid parentheses string s, return the nesting depth of s — the maximum number of nested parentheses.ReadHide
Problem Statement
Given a valid parentheses string s, return the nesting depth of s — the maximum number of nested parentheses.
Input / Output
Input: s = "(1+(2*3)+((8)/4))+1"
Output: 3
Explanation: Digit 8 is inside 3 nested parentheses.
Input: s = "(1)+((2))+(((3)))"
Output: 3
Input: s = "()(())((()()))"
Output: 3
Constraints
1 ≤ s.length ≤ 100
s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'
It is guaranteed that s is a valid parentheses string
Understanding the Problem
(→ go one level deeper)→ come back up one level- Answer = deepest level ever reached
- Other characters don't matter
Edge Cases
- No parentheses → 0
- Input is guaranteed valid → no need to check for unmatched
)
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Counter as Implicit Stack — with only one bracket type, a stack only ever needs its size, so a single integer replaces it.
Approach
Brute Force — Explicit Stack, O(n) space
push on '(', pop on ')', track max stack size
Optimized — Counter, O(1) space
depth = 0, best = 0
for c in s:
if c == '(': depth++, best = max(best, depth)
if c == ')': depth--
return best
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Explicit Stack | O(n) | O(n) | Generalises to multiple types |
| Counter | O(n) | O(1) | One bracket type → just count |
Online Compiler
Solution code
import sys
input = sys.stdin.readline
# ─────────────────────────────────────────────────────────────────────────
# OPTIMIZED — O(n) time | O(1) space
# Only one bracket type → a counter replaces the stack
# ─────────────────────────────────────────────────────────────────────────
def max_depth(s):
depth = 0 # current nesting level
best = 0 # deepest level seen
for c in s:
if c == "(":
depth += 1 # go deeper
best = max(best, depth)
elif c == ")":
depth -= 1 # come back up
return best
# ── Input ─────────────────────────────────────────────────────────────────
s = input().strip()
print(max_depth(s))Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language