Amazon Interview Question for Software Engineer / Developers






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

this will work :-
correct me If i am wrong:

stack s1;
symb = null;
while( (c=getnextsymbol()) != NULL )
{

	if(is_number(c))    push(s1,c)
	
	else if( is_operator(c))
	{       
		if(priority(c)>priority(symb) ) push(s1,c)
		else 
		{ opr2 = s1.pop();
		  op = s1.pop();
		  opr1 = s1.pop();
		  push(s1, evaluate(opr1,op,opr2));
		  push(s1,c);
		}
	 	symb = c;
	}
}

while(numel(s1)>1 )
{
  opr2 = s1.pop();
  op = s1.pop();
  opr1 = s1.pop();
  push(s1, evaluate(opr1,op,opr2));
}

print "ans = " s1.top();

- Aditya October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry that will fail in some cases.

We have to convert the infix expression into postfix and then evaluate the postfix.

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

Your solution is almost there. No need to convert infix to postfix.
- Read next 3 tokens - which will always be number, operator, number
- Add numbers to numStack, operator to opStack

While tokens still left
{
- Read next 2 tokens - number and operator
- Compare operator to topmost op in opStack. If lower in priority, add number to numStack, operator to opStack. If higher in priority, pop number from numStack, evaluate both numbers with operator, push result number onto number stack
}

While (elements still present in opStack)
{
- Pop from opStack, pop 2 numbers from numStack, evaluate. Push result to stack
}

Return result.

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

This seems to be going into an infinite loop if we get an operator whose priority is lower than top of operator stack.

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

My bad.. This is fine.!

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

indirectly u r applying postfix :)

- Anonymous November 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Convert the expression to postfix and then evaluate.

- sunny April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use the stack to do the operations. Will be fine

- weijiang2009 February 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