Amazon Interview Question for Software Engineer / Developers


Team: Kindle-Periodicals
Country: India
Interview Type: In-Person




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

def mirrorTest(t1,t2):
if t1 is None and t2 is None :
return True
if t1 is None || t2 is None :
return False

ileft = mirrorTest(t1.left,t2.right)
iright = mirrorTest(t1.right,t2.left)

if ileft and iright :
if t1.data == t2.data :
return True
return False


#you will call the above function
mirrorTest(root,root)

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

like this solution

- Sourabh March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Do an in-order traverse:
left - self - right
and store the traversed nodes in an array.
2. Do an reversed in-order traverse:
right -self - left
and store nodes in array.
3. Compare two arrays.

This approach is O(3n).

- Larry March 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

in-order traversal does not guarantee a unique tree, for example

1
/ \
2 3
/ \ / \
3 4 2 5
/ /
5 4

- pd March 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool Mirror(node *left,node *root)
{
if(!left && !right)
return true;
if(!left || !right)
return false;

else return Mirror(left->right,right->Left) &&Mirror(left->left,right->right))

}

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

bool Mirror(node *left,node *root)
{
if(!left && !right)
return true;
if(!left || !right)
return false;

else return ((left->data==right->data)&&(Mirror(left->right,right->Left)) &&(Mirror(left->left,right->right))

}

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

Yes this is the correct way to test mirror trees, No need to check inorder

- Anonymous March 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In order traversal of the left and right trees and compare the left string should match the reverse of the right string.

- igan March 19, 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