Amazon Interview Question for Software Engineer / Developers


Country: United States




Comment hidden because of low score. Click to expand.
14
of 16 vote

correction to the above:
bool ISSimilarBST(Node *a, Node *b)
{
if (a == NULL && b == NULL) return true;
if (a == NULL || b == NULL) return false;
return (a->data == b->data &&
ISSimilarBST(a->left, b->left) &&
ISSimilarBST(a->right, b->right));
}

- Vin November 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

issimilar(node *a,node *b)
{
if((a==NULL||B==null)&&(a==NULL&&b==NULL)
return false
else
return true;
issimilar(a->left,b->left);
issimilar(a->right,b->right);
}

- raone November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your two issimlar() statements are unreachable. Vinod's answer is correct.

- Rahul November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

bool ISSimilar(node *a, node *b)
{
if(a==NULL && b==NULL) return true;
else if(a==NULL || b==NULL) return false;
else return (ISSimilar(a->left, b->left) && ISSimilar(a->right, b->right));
}

- explorer.bit November 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the last else part check for the data equality condition also

- Rajan Balakrishnan November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually ... The question says, only to check if two trees are similar.......So we should only check the structure of the tree.
If you'll check for the values also, that means they are copy of each other, which is not required.

- explorer.bit November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

According to the interviewer, the definition of "similarity" is to reflect the data as well as the structure.

- fz November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isSimilar(Node firstRoot, Node secondRoot) {
		
		if ( firstRoot != null && secondRoot != null ) {
			if ( firstRoot.getVal() == secondRoot.getVal() ) {
				return isSimilar(firstRoot.getLeft(), secondRoot.getLeft())
							&&isSimilar(firstRoot.getRight(), secondRoot.getRight());
			} else {
				return false;
			}
		}
		return true;
	}

- belligerentCoder November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool compare_bintree(struct bintree *y,struct bintree *i)
{
if(y==NULL&&i==NULL)
{
//cout<<"NULL binary tree";
return true;
}
else
{
if(y!=NULL&&i!=NULL)
{
if(y->l==i->l)
{
return true&&compare_bintree(y->left,i->left)&&compare_bintree(y->right,i->right);
}
else
{
return false;
}

}
else
{
cout<<"not equal";
return false;
}


}
}

- shubhajoy.das November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Boolean check_similarity(Tree t1,Tree t2){
if(t1==null && t2==null)
return true;
if(t1==null || t2==null)
return false;
return (check_similarity(t1.left,t2.left))&&(check_similarity(t1.right,t2.right));
}

- winner December 12, 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