My first instinct was to just track depth with a counter and update the max as I go.
Use a single-pass counter approach: increment on '(' and decrement on ')', tracking the maximum depth. If the counter ever goes negative or doesn't end at zero, return -1 for unbalanced parentheses.
Pro tip: Clarify upfront whether the input can contain other bracket types or if only parentheses matter, and mention that the counter approach is O(n) time and O(1) space, which is optimal.
Ask if the string can be empty, contain other bracket types, or if unbalanced means mismatched types. Confirm that only parentheses '(' and ')' are considered.
Select a single-pass counter method: maintain current depth and max depth. This is optimal with O(n) time and O(1) space.
During the pass, if current depth becomes negative, return -1 immediately. After the pass, if current depth is not zero, return -1.
Write clean code with clear variable names. Test with cases: empty string, balanced with depth, unbalanced with extra closing, extra opening, and nested.
State time O(n) and space O(1). Mention that a stack-based approach also works but uses O(n) space, so counter is better.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.