Amazon Interview Question for Software Engineer / Developers






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

class expression
{
expression m_data;
public:
expression(int a):m_data(a){}
expression expression::operator+(expression a,expression b);
-,*,/
expression expression::operator()(expression a);
}

- anonymous November 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)change it expression to binary tree which take the operator as the root of each nodes
2)traverse the tree with post-order.
3)traverse the result with stack.
when met the operator,pop up the topmost 2 values and calculate it,then push the result into the stack.

- Anonymous November 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

read the input character by character, first convert the infix expression into a postfix expression using a stack and adhering to precedence rules. Once the postfix expression is ready, evaluate the expression using a stack

- Ace February 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

aaah, this is a piece of cake!!! u can do this in O(n) time...
-create a postfix
-evaluate the result

if u need more email me at chrisogonas@yahoo.com

- Chrisogonas March 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Chrisogonas's comment is quite silly, IMO. Practically useless(the comments before have alluded to postfix notation) and claiming that it is easy is idiotic.

Create postfix: how? Use the shunting yard algorithm.

Evaluation, you push onto stack till you see operator, in which case you pop the required number of arguments which the operator takes from the stack, evaluate and push back the answer on the stack.

- Anonymous March 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. construct (5 * 3) + (4 /2) into a binary tree, where the inner nodes are operators.
2. construct a post order of the tree: 5 3 * 4 2 / +
3. evaluate: push numbers in stack. when meets a operator, pop number of operands this operator needs from the stack, push the evaluation result into stack. Repeat until post order string is finished:
(1)push 5, push 3, meet *, which needs two operands. Pop, pop. Evaluate result is 15, push into the stack.
(2)push 4, push 2, meet /, which needs two operands. Pop, pop. Evaluate result is 2, push into the stack.
(3)meet +, which needs two operands. Pop, pop. Evaluate result is 17. push into the stack.
(4) end of the string. Pop. return as result.

- Anonymous March 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above algorithm assuming the post order traversal is correct, would lead to 15 being pushed into the stack followed by 4 and 2, and then the operator / being encountered.

At this point, 15 will be divided by 4. Not the right result.

- Anonymous March 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above algorithm assuming the post order traversal is correct, would lead to 15 being pushed into the stack followed by 4 and 2, and then the operator / being encountered.

At this point, 15 will be divided by 4. Not the right result.

- Anonymous March 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For evaluation, will an in-order traversal using recursion work fine?

- Anonymous December 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. construct (5 * 3) + (4 /2) into a binary tree, where the inner nodes are operators.
2. construct a post order of the tree: 5 3 * 4 2 / +
3. evaluate: push numbers in stack. when meets a operator, pop number of operands this operator needs from the stack, push the evaluation result into stack. Repeat until post order string is finished:
(1)push 5, push 3, meet *, which needs two operands. Pop, pop. Evaluate result is 15, push into the stack.
(2)push 4, push 2, meet /, which needs two operands. Pop, pop. Evaluate result is 2, push into the stack.
(3)meet +, which needs two operands. Pop, pop. Evaluate result is 17. push into the stack.
(4) end of the string. Pop. return as result

Its correct cuz u pop 4 and 2 which is at top of stack when u see /..result ll be 2 and then it will be added to 15.

- Sourabh March 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How to convert the expression into a binary tree in step 1?

- vkr_coder December 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Be careful this is not a data structure question, this is OO.
So
abstract Class Operation
class Multiplication
class Division
class Addition

all implementing the abstract method calculate()

So algo is :
Operation op1 = new Multiplication(5, 3)
Operation op2 = new Division(4, 2)
Operation op3 = new Addition(op1.calculate(), op2.calculate())

- dantepy May 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good one...thanks

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

result = op3.calculate()

- dantepy May 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a composite design pattern:

class Expression {}

class CompositeExpression: public Expression {
   List<Expression> expression; 
   // This will have one expression, operator and another expression. The expression can be    operand/compositeexpression.
}

class Operand: Public Expression{}

class Operator: public Expression {}

To evaluate start breaking the compositeexpression. do expression operator expression. If the expression is composite, recursively try to evaluate it, till you reach operand.

- Anonymous February 05, 2013 | 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