Amazon Interview Question for Software Engineer / Developers






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

#include <stdio.h>
#include <string.h>
const char* isValidString(char* pStr)
{
	int len=strlen(pStr), index=0, count=0;
	if(pStr[index]==')') return "INVALID STR";
	for(;index<len;index++)
	{
		if(pStr[index]=='(') count ++;
		if(pStr[index]==')') count --;
		if(count < 0) return "INVALID STR";
	}
	return (count == 0)?("VALID STR"):("INVALID STR");
}
int main()
{
	char str[100];
	gets(str);
	printf("%s\n",isValidString(str));
	return 0;
}

- Anonymous June 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- ani June 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a) push when u encouter (
b)pop when u encounter )
c)It means before evry push the stack shud be empty else return false ..

- whataheck June 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

classic

- pansophism June 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

unless if you have to allow nested pairs of ( and )

- Karthik August 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even if nested pairs are there, the stack should be empty when you reach the end of the string

- krannycool September 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@whataheck - Your would not handle case like '((()))'...it is good for case '()()()' only

- Anonymous June 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

he just needs to check at the end whether stack is empty or not and not before every push

- Rakesh Kumar June 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assumption: The string ONLY contains '(' and ')'.

bool check_valid_or_not( string* s)
{
int counter_for_left_paren = 0;
int counter_for_right_paren = 0;
for(int i = 0; i< s.length(); i++)
{
if(s[i] == '(') counter_for_left_paren++;
else counter_for_right_paren++;
if( counter_for_right_paren > counter_for_left_paren)
return false;
}
return (counter_for_left_paren==counter_for_right_paren);
}

Time Complexity:O(n)
Space complexity:O(1)

If anything is wrong, feel free to correct me. Thanks in advance.

- UT June 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey, what about a string "()))((" .
Above string is not wellformed. I prefer to go for using stack data structure

- Srik June 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

my solution will return false with your case you mentioned. what is wrong with that?

- UT June 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the stack approach wud work because the question clearly says .. for every ")" should have a preceding "(".
Hence the case '((()))' need not be considered .Wat say ?

- Whataheck June 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

'preceding' does not necessarily imply that it would be 'immediately preceding' it.
so the condition to be checked is simply 'when you pop, stack should not be empty already'

- someone June 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then in that case as Srik pointed out .. the below will be a wellformed string but which is not ideally .
"()))(("

- whataheck June 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

not at all. think a little more, when you are trying to pop from stack for second "right bracket )" the stack would already be empty. So you return a not-well-formed string. to explain more, here is your string example:

( --> left bracket encountered so pushed in stack
) --> right bracket encountered so popped from stack
) --> stack is empty, no more popping can be done so it would be a not-well formed string and you need not see further.

- someone June 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

store only '(' in stack and for every ')' pop stack and if stack is empty
then it is not welldeformed

also if after traversing the whole string if the stack is not empty
then string is also not welldeformed
u can check for this case '()))(('

- anonymous June 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isWellformedString(final String the_string) {
    Deque<Boolean> stack = new ArrayDeque<Boolean>();
    for (int i = 0; i < the_string.length(); i++) {
      switch(the_string.charAt(i)):
        case '(':
          stack.push(true);
          break;
        case ')':
          if (!stack.isEmpty()) {
            stack.pop();
          } else {
            return false;  //if stack is empty, there wasn't a preceding '('
          }
          break;
    }
    return stack.isEmpty();  //you want an empty stack when the loop is done.
  }

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I forgot to add, that is Time O(n) and Space O(n). I slightly different, more readable form of the first response is:

public boolean isWellformedString(final String the_string) {
    int open_count = 0;
    for (int i = 0; i < the_string.length(); i++) {
      switch(the_string.charAt(i)):
        case '(':
          open_count++;
          break;
        case ')':
          if (open_count > 0) {
            open_count--;
          } else {
            return false;
          } 
          break;
    }
    return (open_count == 0);
  }

This is Time O(n) and Space O(1). Credit to the first poster for this though, this is just a bit more readable in my opinion.

- Anonymous June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean WellFormed(String s)
	{
		int len = s.length();
		int cnt =0;
		for(int i=0;i<len;i++)
		{
			if(s.charAt(i) == '(')
			{
				cnt++;
			}
			if(s.charAt(i)== ')')
			{
				cnt--;
				if(cnt<0) return false;
			}
		}
		return((cnt==0)?true:false);		
	}

- sairamvg June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool wellformed(char a[]){
int i,count=0;
for (i=0;a[i]!='\0';++i){
if(a[i]=='(') ++count;
else if (a[i]==')') --count;
}
if (count==0) return true;
return false;
}

running time O(n)
space O(1)

- Ayush June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define NULL 0
struct node
{
char s;
struct node *next;
};
struct node *top=NULL;
void push(char c)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->s=c;
if(!top)
{
top=temp;
top->next=NULL;
return;
}
temp->next=top;
top=temp;
}
char pop()
{
struct node *temp;
if(!top)
return('0');
else
temp=top;
top=top->next;
return(temp->s);
}
int isempty()
{
if(!top)
return 1;
else
return 0;

}


int IswellFormed(char str[])
{
int n;
int i;
char c;
n=strlen(str);
for(i=0;i<=n-1;i++)
{
if(str[i]=='(')
push(str[i]);
if(str[i]==')')
c=pop();
if(c=='0')
{
printf("not well formed" );
return 0;
}
}
if(isempty())
{
printf("well formed");
}
else
printf("not well formed");
}
int main(void)
{
char str[100];
gets(str);
IswellFormed(str);
return 0;
}

- geeks July 06, 2011 | 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