Balanced Brackets (Medium)
Good morning! Here's our prompt for today.
You may see this problem at Stripe, Apple, Ebay, Square, Snowflake, Digitate, Zoominfo, Sailpoint, Baidu, and Qualtrics.
As a software engineer, you'll often be asked to optimize programs. One of the easiest ways to do so is by the introduction of an additional data structure.
Here's another classic problem along the same vein. We're provided a string like the following: "{[])}"
that is inclusive of the following symbols:
- parentheses '()'
- brackets '[]', and
- curly braces '{}'.
Can you write a function that will check if the symbol pairings in the string follow these below conditions?
- They are correctly ordered, meaning opening braces/symbols should come first.
- They contain the correct pairings, so every opening brace has a closing one.
- They are both of the same kind in a pair, so an opening parenthesis does not close with a closing curly brace.
For example, ()
is valid. ((
is not. Similarly, {{[]}}
is valid. [[}}
is not.
Constraints
- Length of the string <=
100000
- The string will only consist of characters like
[
,]
,{
,}
,(
,)
- Expected time complexity :
O(n)
- Expected space complexity :
O(n)
xxxxxxxxxx
43
var assert = require('assert');
function validateSymbols(str) {
// implement this method
return str;
}
try {
assertIsFunction(validateSymbols);
console.log('PASSED: ' + '`validateSymbols` is a function');
} catch (err) {
console.log(err);
}
try {
assert.equal(validateSymbols('[]'), true);
console.log('PASSED: ' + "`validateSymbols('[]')` should return `true`");
} catch (err) {
console.log(err);
}
try {
assert.equal(validateSymbols('{{[]}}'), true);
console.log('PASSED: ' + "`validateSymbols('{{[]}}')` should return `true`");
OUTPUT
Results will appear here.