Skip to content
Katabench
Try free

Maximum Bracket Depth

Easy About 8 min Free, no account needed

How deep do the parentheses nest? A counter beats a stack.

The brief

Given a balanced string of ( and ) (other characters possible), return the maximum nesting depth.

The allocation budget

Pushing every opener onto a Stack<char> allocates a growing buffer, half a megabyte at the hidden test's 100,000-deep nesting, under a 64 KB allocation budget. You never need the openers' contents, only the depth, so solve it with zero allocations.

Starter code

C#
using System;

public class Solution
{
    // TODO: return the maximum nesting depth of a balanced bracket string.
    public int MaxDepth(string s)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

Stacks
Use last-in, first-out state for nesting, matching and expression evaluation.
String processing
Parse, scan and match text character by character with correct edge cases.

Related katas