DEV Community

Cover image for JS Coding Question #10: Is Balanced Parenthesis [Very Common Question]
Let's Code
Let's Code

Posted on

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

Interview Question #10:

Write a function or program that checks if a string is a balanced parenthesis.🤔

If you need practice, try to solve this on your own without looking at the solution below.

Feel free to bookmark 🔖 even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Codepen:

If you want to play around and experiment with the code: https://codepen.io/angelo_jin/pen/OJgwaed

Solution below uses a stack which is a great algorithm to use in this kind of problem. With a small tweak on the code below, you can solve problem that checks for balanced curly braces, brackets and parenthesis as well.

function isBalanced(str) {
  const stack = []

  for (let char of str) {
    if ( char === '(' ) {
      stack.push(char)
    } else {
      if ( stack.pop() !== '(' ) {
        return false
      }
    }
  }

  if (stack.length !== 0) return false 

  return true
}
Enter fullscreen mode Exit fullscreen mode

Small Cleanup/Refactor

function isBalanced(str) {
  const stack = []

  for (let char of str) {
    if ( char === '(' ) {
      stack.push(char)
    } else if ( stack.pop() !== '(' ) {
      return false
    }
  }

  return stack.length !== 0 ? false : true
}
Enter fullscreen mode Exit fullscreen mode

Happy coding and good luck if you are interviewing!

If you want to support me - Buy Me A Coffee

Video below if you prefer instead of bunch of text/code 👍😊

Top comments (3)

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 • Edited

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.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Why not just:

return !stackLength
Enter fullscreen mode Exit fullscreen mode