## Interview Question

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

I was able to write this way but looks like this is not correct approach

``````public static void main(String args[]) {
int  k = solution (")(");
System.out.println(k);
}

private static int solution(String string) {
if(!string.contains("("))
return string.length();
else if(!string.contains(")"))
return 0;
char[] input = string.toCharArray();
int closeCount = 0;
int openCount = 0;
for (int i = 0; i<input.length;i++) {
if(input[i] == '(')
openCount++;
closeCount = 0;
for(int j =i+1;j<input.length;j++){
if(input[j]  == ')')
closeCount++;
}
if (openCount == closeCount&&openCount!=0&&closeCount!=0)
return (i+1);
}

return 0 ;``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void main()
{
char s[20] ="))";
int i=0,j=strlen(s)-1;

int l=0,r=0,store=j,flagl=1,flagr=1;

while(i<=j)
{

if(s[i]=='('&&flagl==1)
{
l++;
}

if(s[j]==')'&&flagr==1)
{
r++;
}

if(l<r)
{
i++;flagr=0;

}
else if(r<l)
{
j--;
flagl=0;

}
else
{
store=j;i++;j--;flagl=flagr=1;}

}

printf("%d",store);
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````// ZoomBA
/*
Should be solvable by 2 iterations and exactly n space.
You can optimize it later.
Pass 1: At every index position left_bracket_count
-1 : 0 ....
Pass 2: At every index position right_bracket_count
from right, so size : 0 check where left and right count matches.
You are cool.
*/
def find_match_count( string ){
#(mem,count) = fold ( string.value, [list(), 0] ) -> {
\$.p.1 += (\$.o == _'(' ? 1 : 0)
\$.p.0 += \$.p.1 ; \$.p
}
// this is DUMB.. but well..
if ( mem[-1] == 0 ) return #'Index #{size(string)} Count 0'
right_count = 0
i = rindex ( string.value ) :: {
right_count += (\$.o == _')' ? 1 : 0)
mem[\$.i] == right_count
}
i<0 ? 'Nothing Found' : #'Index #{i} Count : #{mem[i]}'
}
println( find_match_count( "(())" ) )
println( find_match_count( "(())))(" ) )
println( find_match_count( "))" ) )``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Below codes works... this code written in python

``````#1. Read array in left to right and count all the opening brackets and assign them into new array

#2. Read Same array from right to left and count all the closing brackets and assign them into new array

#3. Compare 2 new arrays which index no of openings== closings that index is final answer

def Count(s):

print ("-----------------")
Length = len(s)
print ("String:",s,"\nLength of the string:",Length)
print ("-----------------")
Openings = 0
OpeningBracketArray = []
index = []
for i in range(Length):
if(s[i]=="("):
Openings+=1
OpeningBracketArray.append(Openings)
index.append(i)
print ("Openings array:",OpeningBracketArray)
print ("Openings Index:",index)

print ("-----------------")
index = []
Closings = 0
ClosingBracketArray = []
final = 0
for j in range(Length):
if(s[-j-1]==")"):
Closings+=1
ClosingBracketArray.append(Closings)
index.append(j)

ClosingBracketArray.reverse()
print ("Closings array:",ClosingBracketArray)
print ("Closings index:",index)

print ("-----------------")
for k in range(len(OpeningBracketArray)):
if (OpeningBracketArray[k]==ClosingBracketArray[k]):
final=k

print ("at index match: ", final)

return(final)

String = "(())(("
print (Count(String))``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#1. Read array in left to right and count all the opening brackets and assign them into new array

#2. Read Same array from right to left and count all the closing brackets and assign them into new array

#3. Compare 2 new arrays which index no of openings== closings that index is final answer

def Count(s):

print ("-----------------")
Length = len(s)
print ("String:",s,"\nLength of the string:",Length)
print ("-----------------")
Openings = 0
OpeningBracketArray = []
index = []
for i in range(Length):
if(s[i]=="("):
Openings+=1
OpeningBracketArray.append(Openings)
index.append(i)
print ("Openings array:",OpeningBracketArray)
print ("Openings Index:",index)

print ("-----------------")
index = []
Closings = 0
ClosingBracketArray = []
final = 0
for j in range(Length):
if(s[-j-1]==")"):
Closings+=1
ClosingBracketArray.append(Closings)
index.append(j)

ClosingBracketArray.reverse()
print ("Closings array:",ClosingBracketArray)
print ("Closings index:",index)

print ("-----------------")
for k in range(len(OpeningBracketArray)):
if (OpeningBracketArray[k]==ClosingBracketArray[k]):
final=k

print ("at index match: ", final)

return(final)

String = "(())(("
print (Count(String))``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````# Python3.6
def solution(s):
return s.count(')')``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.