Amazon Interview Question for Software Engineer / Developers






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

push if you find '(' and if you find ')' do pop. at the end if the stack in empty then string is correct.

- Anonymous May 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution was...

while(*str)
  if(*str=='(')
    push('(');
  else 
    if(pop() == STACK_EMPTY)
      return FALSE;
  str++;

return TRUE;

- Arang May 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude..according to ur code ... it will accept string like "((((" ....i don't think that is asked in question..let me know if i m wrong!!

- xyz May 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think this would do..

while(*str)
{
if(*str=='(')
push('(');
else
if(pop() == STACK_EMPTY)
return FALSE;
str++;
}
if(pop()==STACK_EMPTY)
return TRUE;
return FALSE;

- xyz May 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not understand why a stack is needed. It is just to increment a counter whenever meeting '(', and decrement the counter whenever ')'. During the whole process, make sure the counter is non-zero; otherwise, return false.

- chenming831@gmail.com May 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So )()( is correct. Think before you comment

- M August 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@M: For your example the counter goes negative in the first step itself.
Think before you comment.

- Mahesh October 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isValid(char *p) {
int i = 0;

while (*(p+i)) {
if (((i & 1) == 0) && (*(p + i) != '(')) {
return 0;
}
if (((i & 1) != 0) && (*(p + i) != ')')) {
return 0;
}
++i;
}
return 1;
}

- d May 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isValid(char *p) {
int i = 0;

while (*(p+i)) {
if (((i & 1) == 0) && (*(p + i) != '(')) {
return 0;
}
if (((i & 1) != 0) && (*(p + i) != ')')) {
return 0;
}
++i;
}
return 1;
}

- d May 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi.
If )( is considered valid, then just do cnt++ for every '(' and cnt-- for every ')'. Otherwise, the running sum should never become <0 and total sum =0.
For checking running sum , either use array or interval trees.

- Sriram May 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right .. if the (()()) is valid, then we can have the code as:

bool isValid(char *p) {
int openB,CloseB,i;
openB=CloseB=i=0;
while (*(p+i)) {
if (*(p + i) != '(')
openB++;
if (*(p + i) != '(')
CloseB++;
if(openB<CloseB)
return false;
}
if(openB==CloseB)
return true;
return false;
}

- jaspreet May 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry, missed the "++i;" statement at the end of the while loop

- jaspreet May 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My attempt for this problem

int func(char* str)
{
int count=0;

// The second condition ensures that ) cannot come before an open (
while(*str!='\0'&& count>=0)
{
if(*str=='(')
count++;
else
count--;
str++;
}

// Equal number of ( and )
if(count==0)
return 1;
else
return -1;
}

- abhimanipal May 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one

- stag September 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

seems incorrect to me

))(( will give 0, though its incorrect
should be like

if(string == '('){
count++
else
if(count > 0){
count --;
}
else{
return false;
}
}
/* finally */
if count!= 0{
return false
}

- Anonymous October 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Add following condition in the loop.

if(count<0) // if the count goes below zero we have received ')' before '('
break;

- Abhishek November 23, 2011 | 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