root@coding-prodigies:~#
← All challenges

Balanced Parentheses

Classic stack problem: does every open paren have a matching close?

🟠 HardDifficulty

The challenge

For each string in ["(()())", "(()", "()()", ")("], print balanced if its parentheses are properly matched, or not balanced if they aren't -- one line per string, in order.

Output

  
🧠 Need a hint? (Python)

Track a depth counter. +1 on '(', -1 on ')' (a for ch in t: loop works well here). If depth ever goes negative, it's unbalanced immediately. At the end, it's only balanced if depth == 0.

🚀

Like solving these?

Structured courses, hands-on exercises, and real multi-file projects are waiting -- your first project unlock is free.

Create a free account

More challenges