If you only have 3 out of 30 candidates pass the bozo test, that limits your choices when it comes to testing them on having any real talent.
If you only have 3 out of 30 candidates pass the bozo test, that limits your choices when it comes to testing them on having any real talent.
Do you really need something as complicated as a stack?
n = 0
for (char in string)
if (char == '(') n++
if (char == ')') n--
if (n < 0) print "Unbalanced"
if (n != 0) print "Unbalanced"But look at what you have: an integer (n) that counts the number of unmatched open parens, that is not allowed to drop below 0, and that can only be incremented and decremented. Your solution there is essentially a pushdown automaton, with n representing your stack of open parentheses (and the value of n representing the size of the stack). If you approached the problem thinking of it as a stack problem, you'd probably end up optimizing to your solution when you realized that actually stacking parentheses could be efficiently simulated with an integer. But the restrictions you've placed on that integer effectively simulate the restrictions of pushing and popping parens off a stack. You're even going through the string once without backtracking.
I'm not sure if it's useful or just comp-sci wankery to think of it this way, but there's an equivalence between that solution and the freshman stack solution after all.