data-structures · beginner · ~15 min
Track a stack depth's high-water mark.
Find how deeply round parentheses are nested in a string.
Implement int max_depth(const char *s) that returns the deepest level of nested round parentheses (). Assume the parentheses are balanced.
s: a NUL-terminated string. Only ( and ) affect the depth; other characters are ignored. The parentheses are guaranteed balanced.
int: the maximum nesting depth (0 if there are no parentheses).
"((()))" -> 3
"()()" -> 1
"(()(()))" -> 3
"abc" -> 0
s: a NUL-terminated string with balanced (); other characters ignored.
int: deepest level of nested parentheses (0 if none).
Parentheses are assumed balanced.
int max_depth(const char *s) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.