Microsoft interview question

Given a string with characters and brackets, return true if the brackets are balanced.

Interview Answers

Anonymous

4 Oct 2015

You should ask which kinds of brackets, and the exact definition of balanced.

Anonymous

13 Oct 2015

Use the Stack data structure. Here's how it'd work: - Whenever an open brace of any type is encountered, store it in the stack - Whenever a closing brace of any type is encountered, check if it matches the type of brace at the top of the stack - If it doesn't or the stack is empty, return "false" - If it matches, pop the stack and then repeat the process till one of the "false" conditions I just stated is encountered or until the end of the string is reached - The fact that you reach the end of the string shows that it's balanced