DEV Community

Discussion on: JS Coding Question #10: Is Balanced Parenthesis [Very Common Question]

Collapse
jayjeckel profile image
Jay Jeckel

I've done hiring interviews more than a few times, so I think there are a few things in this solution that are worth pointing out.

First, one should always be careful about wrapping booleans in conditionals that return booleans as it is one of the basic code smells that put senior devs on guard for what other horrors may lurk in a codebase. If you want a stack length of zero to be true and not-zero to be false, the correct solution is to change the comparison operator, not wrapping it in a ternary.

// code smell
return stack.length !== 0 ? false : true
// no code smell
return stack.length === 0;
Enter fullscreen mode Exit fullscreen mode

On to the main issue. There's no reason to use a stack for this when a counter would work just as well. No need to allocate an array or spend cycles pushing and popping values from it, just increment and decrement a counter, keeping in mind that finding a closing parenthesis when the counter is zero means the parens aren't balanced.

Also, by explicitly checking for the closing parenthesis, this method won't fall over if the input string happens to contain characters other than open and close parentheses.

function isBalanced(str)
{
    let count = 0;
    for (let char of str)
    {
        if (char === '(') { count++; }
        else if (char === ')')
        {
            if (count === 0) { return false; }
            count--;
        }
    }
    return count === 0;
}
Enter fullscreen mode Exit fullscreen mode

In your codepen, the console log tests have an empty string treated as a balanced string while a string with only whitespace is treated as an unbalanced string. This seems inconsistent as both should be considered balanced since they don't have unbalanced parens. My version above correctly registers both cases as balanced.

Lastly, I understand there is some disagreement in the javascript world, but having automatic semicolon insertion do all the work definitely seems like a code smell to me. It isn't hard to put semicolons where they belong, it avoids the possibility of ASI mistakes, and it's one less thing the parser will have to do when it processes your script, so it's definitely best to use semicolons.

Collapse
frontendengineer profile image
Let's Code Author • Edited on

thanks Jay for the snippet and taking time for the explanation you have here.
PS: I like your solution also the return stack.length === 0

5 years ago, I feel the same thing about adding semi-colon everywhere but now I have been coding without it. This might be one of those preference just like spaces and tabs. We all use a build tool and small modules might help that we do not have to worry about ASI mistakes taking place.

If change of heart on this preference, a team can incorporate prettier and it will address semi-colon concerns easily.