Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

int evaluateTree( Node *node)
{
       if (node->left==node-right==NULL)
             return node->data;
       int a= evaluateTree(node->left);
       int b= evaluateTree(node->right);
       return soOperation(node->data, a, b);
}

- jobseeker April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that';s the way to calculate. recursively evaluate the left and right subtree and carryout the operation

- skp April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Any order is fine..Go poop outside..

- Pooper April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You missed a base case. What if you recurse on an operand node with only 1 child, your code will break.
you need to have the base case:
{{
if(node==null) return 0;
}}

to deal with that case. It is possible to have unary operators such as - with only 1 child.

- Srini April 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice answer jobseeker

- DashDash May 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no null conditional check so it breaks horribly i.e. a call like evaluateTree(null) breaks it right away...

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

equals 1 % (5 + 8) or 1 % 5 + 8?
in-order traversal?

- milo April 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

any order is fine, he want's a value

- pga April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Any order is fine" ... I don't know what to say to that. The value depends on the order, ya' know? You might as well just say "any value is fine", then...

- eugene.yarovoi April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hmm root node is %, root-left node is 1 and root-right is "+".

root-right-left node is '5' and right is '8'.

so he wanted 1%(5+8)....i could not get this so in the end he said any order is fine..hope this clears

- pga April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to do a pre order traversal and push the elements you get out of the traversal into a stack. So you will get a prefix notion like % 1 + 5 8. Now pop the elements out of stack one by one till you get an operand. Store the numbers you popped out in a temp list. When ever you get an operand, apply the operation on the previously popped out numbers and push the value back into the stack. Do this till you pop out every element from the stack.

Hope this helps

- john April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Were you able to arrive at an acceptable solution?

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

Do a post order recursive traversal and return the value calculated for the subtree under each node

- Renjith April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yep that is what i did, problem is I just took it into a stack

- pga April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yep that is what i did, problem is I just took too long

- pga April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

do a inorder traversal and store them in a queue and just dequeue and calculate.

please correct if i am wrong.

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

if the expression is "a+b*c+d".its preorder is "++a*bcd"
now iterate through the preorder expression in reverse order.
i.e in order dcb*a++.
while iterating if you get operand push on to stack.if it is an operator pop the stack take it as first elment and pop the stack once again to get second element.apply the operator on first element secdond element.then push it on to the stack.
in above example
push(d)
push(c)
push(b)
temp=pop()*pop()
push(temp)
push(a)
temp=pop(a)+pop(b*c)
push(temp)
pop()+pop()
this is the final result

- valluri.ravindra May 09, 2012 | 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