Challenge: Check Parentheses

This challenge will test your skills of writing efficient code using functions in JavaScript.

Problem statement

Let’s say your teammate is working on a JavaScript code that won’t run because the brackets (square, curly, round) are not properly nested. You then decide to write a Parentheses Checker function, which efficiently checks whether or not the input string’s opening and closing brackets are correctly nested.

Note: Two brackets are considered a matched pair if the opening bracket occurs to the left of the closing bracket of the exact same type.

Input

  • A string containing opening and closing brackets

Output

  • A boolean value-true or false

Sample input

"{ [ ] ( ) }"
"{ [ ( ] ) }"

Sample output

true
false

Challenge

Take a close look and design a step-by-step algorithm before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good luck!

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