Created
February 22, 2017 13:08
-
-
Save abhinavnigam2207/f6df6e8a1d95f248edb7762360d9d901 to your computer and use it in GitHub Desktop.
Bracket Problem (Asked in Toptal Interview )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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; | |
} |
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
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.