For the challenge, a one-liner solution with variations was proposed:
def solve(string):
s = ''.join([ch for ch in string if ch in '()'])
return len(max(s.split(')'), key=len))


Does it work? No.
If we feed this function the string (()(())), the correct answer (depth) is 3, but the function returns 2. The logic of "consecutive opening brackets" breaks on any nested structures that are not the first branch.

☝🏻 How to solve correctly
A boring but working solution with O(n) complexity uses a simple counter. We traverse the string, increment the counter on (, decrement on ), and update the global maximum at each step.

def max_depth(s: str) -> int:
current_depth = 0
max_depth = 0

for char in s:
if char == '(':
current_depth += 1
max_depth = max(max_depth, current_depth)
elif char == ')':
current_depth -= 1
# Validation (optional)
if current_depth < 0:
return -1

return max_depth if current_depth == 0 else -1


For the aesthetically inclined (functional):

If you really want a one-liner, you can use accumulate. Map brackets to 1 and -1, compute prefix sums (accumulate), and take the maximum.

from itertools import accumulate

def solve_poly(s: str) -> int:
# Convert '(' to 1, ')' to -1, others to 0
depths = accumulate(1 if c == '(' else -1 if c == ')' else 0 for c in s)
return max(depths, default=0)


#algosobes