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 ;

}

- xyz March 17, 2017 | Flag Reply
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);
}

- Ashutosh Singh March 18, 2017 | Flag Reply
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( "))" ) )

- NoOne March 18, 2017 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More