Amazon Interview Question for Applications Developers


Country: India
Interview Type: Written Test




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

initialize i = 0
traverse the string:
If you find open braces => i++
else if closing braces => i--

in the end i should be 0 + i should not be negative at any instant.

- Cerberuz August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your approach won't work for this case or other similar cases:
)(

The idea should be to use a stack or a count left_count and right_count and at point in time right count should never be greater than left count and in the end both should be zero.

- mr August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Read my comment carefully( last statement) . If i is negative at any instant then brackets wont match.
)( => i will be -1 for first ')' => brackets wont match.

- Cerberuz August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static boolean isNested(String AK){
int status=0;
for(int i=0;i!=AK.length();i++){
if(AK.charAt(i)=='('){
status++;
continue;
}
if(AK.charAt(i)==')'){
status--;
if(status<0){
return false;
}
continue;
}
}
return status==0 ? true : false;
}

- Chengyun Zuo August 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Cerberuz
Sorry i missed that part but still i have one more scenario which i had in mind while posting the stack based approach where in the left and right parenthesis could be from "{, [ , ( , ), ] , }" set.

In that case keeping counters won't help i think. Rather keep pushing the left parenthesis on the stack and when a right parenthesis comes in check the top of the stack for corresponding left parenthesis and pop it till string gets emptied. At that point in time stack should also be empty.



Opinions?

- mr August 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int valid_parenthesis(string str)
{
	 int size_str = str.size();
	 stack<char> stk;
	 for(int i=0; i<size_str; i++)
	 {
		assert(str[i]=='(' || str[i]=='{' || str[i]=='[' || str[i]==')' || str[i]=='}' || str[i]==']');
		
		if(str[i]=='(' || str[i]=='{' || str[i]=='[')	stk.push(str[i]);
		else {
			if(str[i]==')' && stk.top()!='(') ||
				str[i]=='}' && stk.top()!='{') ||
				str[i]==']' && stk.top()!='[') ) {
				return false;
			}
		}
	}
	
return stk.empty() ? true:false;
}

- mr August 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You still don't require stack, use 3 variable i,j,k for each type '(', '{', '['. Moreover with 3 types, this problem is more complex.

You need to take care of invalid cases like :
{ { [ } ] }

- Cerberuz August 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And how will you take care of those invalid cases without a stack or something conceptually equivalent?

- eugene.yarovoi September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

1)whenever we get '(' push it in stack
2)whenever we get ')' pop
3)At any point of time if pop is returning -1(i.e; we are trying to pop from empty stack i.e; number of ')' is greater so return false
4)at end if stack is empty return true

- samrat August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool valid_braces(std::string str) {
  int open = 0;
  for (int i = 0; i < str.length(); i++) {
    if (str[i] == '(') open++;
    else if (str[i] == ')') {
      if (open == 0) return false;
      else open--;
    }
  }
  return true;
}

- Martin August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Martin I think "SAM((RAT" type of string is not getting Handled, so we can modify your code as..
let me know if wrong...
bool valid_braces(std::string str) {
  int open = 0,close=0;
  for (int i = 0; i < str.length(); i++) {
    if (str[i] == '(') open++;
    else if (str[i] == ')') {
      close++;
    }
  }
if(close==open)
  return true;
else 
 return false;
}

- samrat August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@samrat ur approach ll fail for the cases like tis. "())(". It will return true for tis case whereas it shud return false.

- Anonymous August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Adding to Cerberuz,Martin,Samrat approach....how abt additional check...checking the last peranthesis character

If its (, even though string has balanced paranthesis..that should be an invalid string

- Dayanand August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bool valid_braces(std::string str) {
int open = 0,close=0;
for (int i = 0; i < str.length(); i++) {
if (str[i] == '(') open++;
else if ((str[i] == ')') {
if(close>open)
return false;
close++;
}
}
if(close==open)
return true;
else
return false;
}

- samrat August 26, 2012 | Flag


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