Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

bool Equal(Node a, Node b) {
  if (a == null && b == null) return true;
  if (a == null || b == null) return false;
  return a.value == b.value
    && Equal(a.left, b.right)
    && Equal(a.right, b.left);
}

- Mike1324 December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the exact solution i have given..and then he asked me about run time complexity ...i said O(n)

boolean tree_compare(Node t1, Node t2){
if(t1==null && t2 == null) return true;
if(t1 ==null || t2 == null) return false;
return (t1.val == t2.val) && tree_compare(t1.left, t2.right) && tree_compare(t1.right, t2.left)

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

i think mike your solution will just compare two similar trees....i think this is not for finding whether two binary trees are mirror images of each other....

- Minnu December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah I misunderstood what mirror meant - fixed

- Mike1324 December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

An mirror image is where arranged with a reversal of right and left

- Minnu December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

boolean tree_compare(Node t1, Node t2){
if(t1==null && t2 == null)
return true;
if(t1 ==null || t2 == null)
return false;
return (t1.val == t2.val) && tree_compare(t1.left, t2.right) && tree_compare(t1.right, t2.left)

- Minnu December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome!! I was just thinking in that way! you got it spot on!

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

BOOL Mirror(Node *T1, Node *T2) {
    if(T1==NULL && T2==NULL) return TRUE;
    if(T1==NULL || T2==NULL) return FALSE;
    return ((T1->info == T2->info) && 
        Mirror(T1->Left, T2->Right) && 
        Mirror(T1->Right, T2->Left));
}

- SRB December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about in order traversal on both trees? and then comparing the output? Would it not be in O(n)?

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

Do the in order and pre order traversal of both of trees and then just compare the results.

Time Complexity = O(N+N)

- Usman December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find Top to Bottom and left to right traverse in one tree....
and Top to Bottom and right to left traverse in another tree...
if both are same then tree have mirror image ....!!

- Mukesh Kumar Dhaker January 17, 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