Challenge 2: Balance Parenthesis

Given an array that contains opening and closing parentheses, check whether or not the opening and closing parentheses are balanced.

Problem Statement

Implement a function that takes an array testVariable containing opening ( and closing parenthesis ) and determines whether or not the brackets in the array are balanced. The function also takes startIndex = 0 and currentIndex = 0 as parameters.

What does “Balanced Parenthesis” Mean?

Balanced parentheses mean that each opening bracket ( has a corresponding closing bracket ). Also, the pairs of parentheses are properly nested.

Consider the following correctly balanced parentheses:

()()

(())(())

(())()(())()

((()))((()))((()))((()))

Now take a look at some incorrectly balanced parentheses:

((

)()()()(

((()()()()((()()()()

((())))((((()((())))((((()

Input

An array testVariable containing opening and closing parentheses.

Output

true if the parentheses in the input array are balanced. false if the parentheses in the input array are imbalanced.

Sample Input

testVariable = ["(", ")", "(", ")"]

Sample Output

true

Try it Yourself

Try this challenge yourself before examining the solution. Good luck!

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.