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.
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
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.
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
- Decode String Medium Free Expand k[encoded] patterns, nesting included.
- Simplify Unix Path Medium Free Canonicalize a Unix path - and survive the .. that climbs past root.
- Add Binary Strings Easy Free Add two binary numbers far too long for any built-in integer.
- Base64 Decoded Length Easy Free The decoded size is pure arithmetic - no need to decode.