Find the maximum nesting depth of parentheses in an arithmetic expression string

Quick Overview

Given a valid parentheses expression made of digits, arithmetic operators and parentheses, return the maximum nesting depth of its parentheses. It tests single-pass string scanning, constant extra space, and reasoning about a recursively defined quantity.

Find the maximum nesting depth of parentheses in an arithmetic expression string

Company: Intel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A string is a valid parentheses string (VPS) if it meets one of the following: - It is the empty string `""`, or a single character other than `"("` and `")"`. - It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are both VPS. - It can be written as `(A)`, where `A` is a VPS. The nesting depth `depth(S)` of a VPS `S` is defined the same way: - `depth("") = 0` - `depth(C) = 0`, where `C` is a single character other than `"("` and `")"`. - `depth(A + B) = max(depth(A), depth(B))`, where `A` and `B` are VPS. - `depth("(" + A + ")") = 1 + depth(A)`, where `A` is a VPS. For example, `""`, `"()()"` and `"()(()())"` are VPS with nesting depths 0, 1 and 2, while `")("` and `"(("` are not VPS. Given a VPS as the string `s`, return its nesting depth. ### Function Signature ```python def max_depth(s: str) -> int: ``` ### Rules - `s` is guaranteed to be a VPS, so every `(` has a matching `)` later in the string. - Digits and operator characters do not change the depth. ### Constraints - `1 <= len(s) <= 100` - `s` consists of the digits `0-9` and the characters `'+'`, `'-'`, `'*'`, `'/'`, `'('` and `')'`. - `s` is a VPS. ### Examples ```text Input: s = "(1+(2*3)+((8)/4))+1" Output: 3 ``` The digit `8` sits inside three open groups at once: the outermost group, the group `((8)/4)`, and the group `(8)`. No character is inside more than three groups, so the depth is 3. ```text Input: s = "(1)+((2))+(((3)))" Output: 3 ``` The three parts have depths 1, 2 and 3, and the depth of a concatenation is the largest depth among its parts. ```text Input: s = "8*3" Output: 0 ``` There are no parentheses, so every character is at depth 0.

Overview: Given a valid parentheses expression made of digits, arithmetic operators and parentheses, return the maximum nesting depth of its parentheses. It tests single-pass string scanning, constant extra space, and reasoning about a recursively defined quantity.

Intel logo
Intel
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A string is a valid parentheses string (VPS) if it meets one of the following:

  • It is the empty string "" , or a single character other than "(" and ")" .
  • It can be written as AB ( A concatenated with B ), where A and B are both VPS.
  • It can be written as (A) , where A is a VPS.

The nesting depth depth(S) of a VPS S is defined the same way:

  • depth("") = 0
  • depth(C) = 0 , where C is a single character other than "(" and ")" .
  • depth(A + B) = max(depth(A), depth(B)) , where A and B are VPS.
  • depth("(" + A + ")") = 1 + depth(A) , where A is a VPS.

For example, "", "()()" and "()(()())" are VPS with nesting depths 0, 1 and 2, while ")(" and "((" are not VPS.

Given a VPS as the string s, return its nesting depth.

Function Signature

def max_depth(s: str) -> int:

Rules

  • s is guaranteed to be a VPS, so every ( has a matching ) later in the string.
  • Digits and operator characters do not change the depth.

Constraints

  • 1 <= len(s) <= 100
  • s consists of the digits 0-9 and the characters '+' , '-' , '*' , '/' , '(' and ')' .
  • s is a VPS.

Examples

Input: s = "(1+(2*3)+((8)/4))+1"
Output: 3

The digit 8 sits inside three open groups at once: the outermost group, the group ((8)/4), and the group (8). No character is inside more than three groups, so the depth is 3.

Input: s = "(1)+((2))+(((3)))"
Output: 3

The three parts have depths 1, 2 and 3, and the depth of a concatenation is the largest depth among its parts.

Input: s = "8*3"
Output: 0

There are no parentheses, so every character is at depth 0.