Amazon Interview Question for Software Engineer / Developers






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

Here is my code

booolean isSymmetric(node n)
{
	return isMirrorImage(n,n);
}
boolean isMirrorImage(node n1,node n2)
{
	if(n1==null && n2==null)
		return true;
	else if(n1!=null || n2!=null)
		return false;
	else if(n1.val !=n2.val)		
		return false;
	else 
		return isMirrorImage(n1.left,n2.right) && isMirrorImage(n1.right,n2.left);
}

- gulusworld1989 February 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution. well done gulusworld1989.

- akash.kotadiya2000@gmail.com June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Amazing idea. But I think this clause is not correct

else if(n1!=null || n2!=null)

It would make the initial call thru isMirrorImage(root,root) return right away. It won't go into any recursion.

- Vikas October 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is Symmetric means mirror image should be same??

- Tulley February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes

- siva.sai.2020 February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;

/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};

/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

vector <int> v;
int index=0;

/* Given a binary tree, print its nodes in inorder*/
void Preorder(struct node* node)
{
if (node == NULL)
return;

v.push_back(node->data);
index++;

/* then recur on left sutree */
Preorder(node->left);

/* now recur on right subtree */
Preorder(node->right);
}

/* Driver program to test above functions*/
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(3);
root->right->right = newNode(4);

Preorder(root);

int sym=0;
for(int i=1;i<index;i++)
{
sym ^= v[i];

}
if( !sym ) cout<< "symmetric"<<endl;
else cout<<" NOT Symmetric "<<endl;

getchar();
return 0;
}

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

I think you shouldn't be XORing the data in the root of the tree.

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

oh! the index i is starting from 1. never mind.

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

How can xoring all elts determine symmetricity???
consider this

1
  2  3
 3    2

I believe it should be BFS and not DFS for this q...

- Sathya February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this code having something wrong ..
I think after XORing .. if the 'sym' equal to the "root value" it would be semetric .. otherwise it is not.

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

I think this will only guarantee the value not the structure of the tree

- vv February 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean isSymetry(Node root) {
return isSymetry(root.right, root.left);
}

boolean isSymetry(Node n1, Node n2) {
if(n1 == null && n2 == null) {
return true;
}
if(n1 != null && n2 != null) {
return n1.data == n2.data && isSymetry(n1.right, n2.left) && isSymetry(n1.left, n2.right);
}
return false;
}

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

Brilliant!I thought of doing BFS and check for palindrome.But this is far better!!

- Sathya February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sweet and simple...amazing...!

- PKT February 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work bcoz.. the algorithm just compares nodes left and right children.. we need to check for symmetry for all the node in a given level... only thing i can think of is to check if the in-order traversal string is palindrome..

- Anonymous Coward February 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Coward,

Can you please explain why it wont work. I am not clear on the part where the algorithm checks for symmetry on a level

- Aniket March 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah, thanks for sharing the pseudocode.
Coward, could you give some example where the logic would fail?

- Fanatic May 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it wont work with any case....
see this 1
2 2
3 3 4 4
in ur code this will symmetric but in reality it is not ..

- CowaRd May 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

are you gone mad... see the solution properly....
It is correct solution

- swathi July 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great...
But how u come up with suc ideas??

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

Do an inorder traversal. If result is a pallindrom then symmetric else not symmetric.

- SS February 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work. Here is an example:

10
   /
  5
 /
10

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

I think we need to enhance your logic with

depth of left subtree == depth of right subtree &&
preorder traversal should be a palindrome

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

My previous comment is wrong. Found an example where both conditions are true but tree is not symmetric.

10
   /  \
  5   10
 /    /
10   5

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

Algorithm:
1. Traverse the tree in in-irder traversal & generate string.
2. Check the string created above is palindrome or not. If it is palindrome then then tree is a symmetric.

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

This approach does not work because Inorder in itself does not lead to unique tree structure. eg

1
            2                       5
     3                       2            3
4        5                                     4

has inorder as 435212534 which is a paindrome but the tree is nowhere near symmetric

- Abhishek April 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think a more foolproof approach would be

to check symmetricity at Node root

Inorder(root.left) == rev(Inorder(root.right))
&&
Preorder(root.left) == rev(Postorder(root.right))

I have not checked all the edge cases yet, but this seems to be working for all the simple cases I used.

- Abhishek April 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Two solutions:
1. BFS, and put a delimiter after the nodes on each level. So when one level is done check if its next level is mirror.
2. Besides the root. if the tree is symmetric, then its left subtree should be identical to its right subtree. The problem becomes: checking if two trees are identical or not. Which can be done easily (e.g. pre-order traverse the two subtrees at the same time).

- AW April 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solutions
thnx...:)

- Naya Panchi May 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your Solution is right, but i think it needs slight modification... think of such a case:
1
2 2
3 3

The 3rd level is symmetric on its own, but overall, tree is not...
we can change your solution slightly to cater such cases. whenever you encounter a NULL, put some known character, say -1 in case of tree with +ve integers. We can stop the algorithm if we see a case of non symmetry or get a Layer with all -1's !!

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

Sorry the formatting is not evident... in the above example, both 2's don't have a left child, and 3's are their right children..

- meetsaurabhsrivastava July 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there any algo to solve this

1
	  2				 2
   3		    4 	           4		 3
5	6 	7 	8	8    7    6	    5

- Remington June 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

preorder is reverse to its postorder

- Anonymous February 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