Amazon Interview Question for Software Engineer / Developers






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

from the root node, do the broad first seach for the two sub nodes. At each step, put the node into a queue. if a node is NULL, put NULL into the queue also.
After that,we got two queques. Compare these two queues side by side. Return false when the two compared node are not equal. Return true at the end

Time: O(2*N) Space: O(2*N)

- majun8cn October 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

for n-ary tree, do the same thing. Got N queues.
Always add NULL to the queues if a node does not exist

- majun8cn October 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for the queue holding left branch nodes, add leftmost node first; and for the queue holding right branch nodes, add rightmost node first.

- Anonymous October 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

we can mirror a binary tree and then check the inorder before and after the mirroring....... if it is same then it is a symmetric binary tree else not

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

and your definition of symmetry?

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

both shape and data

- SK October 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

does symmetric with respect to data mean that the left half of the tree is mirror image of right half

or

for every node its left and right node will be the same??

- anon October 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This func takes left & right child of a tree, which is going under symmetry consideration.

bool isSymmetricTree(node * ptr1, node * ptr2)
{

    if(ptr1->value != ptr2->value)
        return 0;

    return ( ( ( ptr1->left != 0 && ptr2->left != 0 ) ? isSymmetricTree(ptr1->left, ptr2->left) : (ptr1->left == ptr2->left) )
             && ( ( ptr1->right != 0 && ptr2->right != 0 ) ? isSymmetricTree(ptr1->right, ptr2->right) : (ptr1->right == ptr2->right) ) );
}

- Ashutosh October 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the last line should change to return ( ( ( ptr1->left != 0 && ptr2->right != 0 ) ? isSymmetricTree(ptr1->left, ptr2->right) : (ptr1->left == ptr2->right) )
&& ( ( ptr1->right != 0 && ptr2->left != 0 ) ? isSymmetricTree(ptr1->right, ptr2->left) : (ptr1->right == ptr2->left) ) );

Because symmetric means mirroring , the left sub tree should mirror the right sub tree.
}

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

How about creating in-order, postorder or preorder traversal output of left and right sub tree of root node and comparing all the elements one by one.

any thoughts???

- Ankur October 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if there are million nodes then the time and space complexity increases. :) Better to write an algorithm such that while traversing itself it checks all the conditions :)

- Ankur Soni February 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void mirror(Node node) {
if (node != null) {
// do the sub-trees
mirror(node.left);
mirror(node.right);
// swap the left/right pointers
Node temp = node.left;
node.left = node.right;
node.right = temp;
}
}

- sourabh October 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//lets use preorder traversal technique 4 dis
p=root->l;
q=root->r;

for(;p!=NULL&&q!=NULL;)
{ if(p->r!=NULL&&q->l!=NULL)
{ push(s1,p->r); push(s2,q->l);
}
if(p->l!=NULL&&q->r!=NULL)
{ p=p->l;
q=q->r;
}

else
{p=pop(s1);
q=pop(s2);
}
}

if(p==q)
return "SYMMETRIC";
return "NOT SYMMETRIC"

- mindcoder January 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can also go for a diferent strategy.First we go for the in-order traversal of the Left subtree and push every node->data into a stack. Then we make and in-order traversal of the Right subtree and compare the data with the popped value of stack.If they don't match then the right subtree is not a mirror image of the left subtree and so tree isn't symmetric

- Anonymous February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can also go for a diferent strategy.First we go for the in-order traversal of the Left subtree and push every node->data into a stack. Then we make and in-order traversal of the Right subtree and compare the data with the popped value of stack.If they don't match then the right subtree is not a mirror image of the left subtree and so tree isn't symmetric

- Anonymous February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

traverse the tree by inorder traversal and reverse inorder traversal and compare the sequence.

- Anonymous June 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isSymmetric(Node *ptr1, Node *ptr2)
{
if (!ptr1 && !ptr2)
return true;
if (!ptr1 || !ptr2)
return false;
return((ptr1->data == ptr2->data) && isSymmetric(ptr1->left, ptr2->right) && isSymmetric(ptr1->right, ptr2->left));
}

- Aditya June 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// pass root->left and root->right in this function
bool Symmetric_tree(node *ptr1,node *pt2)
{
if(!ptr1 && !ptr2)
return(true);
if(!ptr1)
return(false);
if(!ptr2)
return(false);
return((ptr1->data == ptr2->data)&&Symmetric_tree(ptr1->left,ptr2->right)&&Symmetric_tree(ptr1->right,ptr2->left))
}

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

From main call the function isSymmetric1 with the root node as argument

bool isSymmetric1 (Node *root)
{
    if (root == NULL)
        return true;
    for (int i = 0, int j = root->no_of_children - 1; i < j; i++, j--)
    {
        if (!isSymmetric2 (a->children[i], a[children[j])
            return false;
    }
    return (i == j)? isSymmetric (a->children[i]): true;
}

bool isSymmetric2 (Node *p, Node *q)
{
    if (p == NULL && q == NULL)
        return true;
    if ((p != NULL && q != NULL) && (p->info == q->info) && (p->no_of_children  ==                q->no_of_children)
    {
        for(int i = 0, int j = no_of_children - 1; j >= 0; i++, j--)
        {
            if (!isSymmtrical2 (p->children[i], q->children[j])
                return false;
        }
        return true;
    }
    return false;
}

- Siraj January 15, 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