Skip to content

Instantly share code, notes, and snippets.

@abhinavnigam2207
Created February 22, 2017 13:08
Show Gist options
  • Save abhinavnigam2207/f6df6e8a1d95f248edb7762360d9d901 to your computer and use it in GitHub Desktop.
Save abhinavnigam2207/f6df6e8a1d95f248edb7762360d9d901 to your computer and use it in GitHub Desktop.
Bracket Problem (Asked in Toptal Interview )
'use strict';
function solution(S) {
var arr = [];
var K = 0;
for (var i=0; i<S.length; i++) {
if (S[i]=='(') {
arr.push(S[i]);
} else {
if(arr.length){
arr.pop();
}
K++;
}
}
return K;
}
@abhinavnigam2207
Copy link
Author

You are given a string S containing of N brackets, opening"(" and/or closing ")". The goal is to split S into two parts (left and right), such that the number of opening brackets in the left part is eaual to the number of closing brackets in the right part. More formally we lookingfor an integer K such that:

0<=K<=N, and
the numbers of opening brackets in the K leading characters of S is the same as the number of closing brackets in the N-K training characters of S.
Write a function:
function solution(S);

that, given string S, returns a value for K that satisfies the above conditions. It can be shown that such a number K always exists and is unique.

@prrraveen
Copy link

Hi abhinav,
Thanks for sharing. what were the other two problems? I am preparing for Toptal. I would like to know the difficulty level of problems asked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment