Amazon Interview Question for Software Engineer in Tests






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

chkSim(root r1,root r2)
{
return (r1->left.data==r2->right.data)&&
(r2->left.data==r1->right.data)&&
chkSim(r1->left,r2->right)&&
chkSim(r1->right,r2->left)
}

- Anonymous August 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution

- neo August 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Need to check NULL

bool isSymmetric(Node *r1, Node *r2)
{
    if (r1 == NULL && r2 == NULL)
        return true;
    
    if (r1 == NULL || r2 == NULL)
        return false;
        
    return (r1->data == r2->data && isSymmetric(r1->left, r2->right) && isSymmetric(r1->right, r2->left));
}

- Anonymous August 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent !!

- ACP Pradyuman September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It says tree, not binary tree!

- Anon October 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i little confused here !! whether u people understood the question wrong or the myself! here tree is symmetric means its mirror image should be exactly the same as the tree.
but you people writing code to test whether r1 and r2 are mirror images rather than testing they should be same.
i.e r1.left,r2.left should be same,similarly r1.right,r2.right should be same.indirectly it says that the tree's left and right subtrees should be mirror images.

- raghu December 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a mirror image of the tree and then check whether the original tree is equal to the mirror image. If both the trees are equal then the tree is symmetric

- Rids August 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution..

- phoenix August 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't work for some cases. Imaging

1
1

it's not symmetric.

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

any kind of traverse,
one left first, one right first, compare one by one

- songofsp3 August 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static boolean chksym(BTreeNode r1, BTreeNode r2) {
// TODO Auto-generated method stub
if((r1.left==null&&r2.right==null)&& (r1.right==null&&r2.left==null)||(r1.left==null&&r2.right==null)||(r1.right==null&&r2.left==null))
return true;
else if(r1.left==null&&r2.right==null&&r1.right!=null&&r2.left!=null)
return ((r1.right.val==r2.left.val)&&(chksym(r1.right,r2.left)));
else if(r1.right==null&&r2.left==null&&r1.left!=null&&r2.right!=null)
return ((r1.left.val==r2.right.val)&&(chksym(r1.left,r2.right)));
else if((r1.left!=null&&r2.right!=null)&& (r1.right!=null&&r2.left!=null)&&(r1.left!=null&&r2.right!=null)&&(r1.right!=null&&r2.left!=null))
return ((r1.left.val==r2.right.val)&&(r1.right.val==r2.left.val)&&(chksym(r1.left,r2.right))&&(chksym(r1.right,r2.left)));
else return false;

}

- rash August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a breadth first travel simultaneously on two trees and at each step check if one traversal is the reverse of the other. If it is then the two trees are symmetric, or else they are not.

- Srikanth August 09, 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