Amazon Interview Question for Consultants


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
10
of 12 vote

public boolean recurseSymmetry(Node<AnyType> left, Node<AnyType> right ){
if(left == null || right == null) return left==right;
else 
return left.value == right.value && recurseSymmetry(left.left, right.right) && recurseSymmetry(left.right, right.left);
}

- krish July 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You're only checking the outer nodes

- oOZz July 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please disregard my previous post. I didn't see that the screen scrolls to the right. This solution works! +1 from me.

- oOZz July 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the complexity of this solution?

- aopencv July 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code will not work !

- vjszq4 July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Inorder traversal output should be a palindrome for a symmetric btree. O(n).

- farabi.dhaka October 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Recursive checking
1. Each node contains 2 leaves, if none then all height must equal

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

main()
{
p=root->leftchild
q=root->rightchild
identical(&p,&q)
}
identical(struct btreenode*a,struct btreenode*b)
{
if((a==null&&b==null)
return true;
else if (a!=null&&b!=null)
{
return (a->data==b->data&& identical(a->left,b->right)&&identical(a->right,b->left);
}
else return false;
}

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

Your code will not work !!

- vjszq4 July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isSymmetric (BTNode current) {
		if (current == null) {
			return false;
		}

		if (current.left() != null && current.right() != null) {
			return isSymmetric(current.left()) && isSymmetric(current.right());
		} else {
			if (current.left() == null && current.right() == null) {
				return true;
			}
		}

		return false;
	}

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

Do a left->root->right in-order traversal(t1), and a right->root->left in-order traversal(t2), and a left->right->root postorder traversal (t3) and a right->left->root postorder traversal(t4)
the tree is symmetric iff the results of t1==t2 && t3==t4
It's not the most efficient way, but it's easy to implement

- cliffyq July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it is a applicable way to find the answer! excellent!

- ctang January 22, 2014 | Flag


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