Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

int sameMirroredTree(struct node* a, struct node* b)
{ 
     // 1. both empty -> true 
     if (a==NULL && b==NULL) return(true);
     // 2. both non-empty -> compare them 
     else if (a!=NULL && b!=NULL) 
     { 
       return( 
                  a->data == b->data && 
                              sameMirroredTree(a->left, b->right) && 
                              sameMirroredTree(a->right, b->left) 
                                                                                              ); 
      }
      else
      return false;

- hello world February 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This should do:

bool IsMirror(Node* me, Node* myMirror)
{
       return (
				  (me != NULL && myMirror != NULL) && 
				  (me.Value == myMirror.Value)       &&
				  IsMirror(me->Left, myMirror->Right) &&
				  IsMirror(me->Right, myMirror->Left) &&
				 );
}

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

base condition??

- Duh!! February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int isSymmetric( struct Node *root1, struct Node *root2 )
{
if( root1 == NULL && root2 != NULL || root1 != NULL && root2 == NULL )
return 0;
if( root1 == NULL && root2 == NULL )
return 1;
if( root1->val != root2->val )
return 0;
if( isSymmetric( root1->left, root2->right ) && isSymmetric( root1->right, root2->left ) )
return 1;
else
return 0;
}

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

nice one.. thanks!

- maddy February 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def isMirrorBST(t1, t2):
 if t1 is null && t2 is not null: return false
 if t1 is not null && t2 is null: return false
 if t1 is null && t2 is null: return true
 if t1.data == t2.data:
   return isMirrorBST(t1.left, t2.right) and
          isMirrorBST(t1.right, t2.left)

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

The recursive solution is straight forward. But to do this non recursively, you need to verify two sets of straight and mirrored traversals are the same - we check pre-order and post order here.

bool CheckMirroringRecursive( NODE tree1, NODE tree2 )
{

	if( tree1 == NULL && tree2 != NULL )
		return false;
	
	if( tree1 != NULL && tree2 == NULL )
		return false;
	
	if( tree1->data != tree2->data )
		return false;
	
	return CheckMirroringRecursive(tree1->left, tree2->right) && CheckMirroring(tree1->right, tree2->left)
		return false;
}
bool CheckMirroringNonRecursive( NODE tree1, NODE tree2 )
{
	_stack stack1, stack2;
	NODE temp;
	NODE nextPreOrderNode = NULL, nextInOrderNode = NULL;
	do
	{
		nextPreOrderNode = NextPreOrderSuccessor(tree1, stack1, true);
		nextInOrderNode = NextInOrderSuccessor(tree2, stack2, temp, true);
		if( nextPreOrderNode == NULL && nextInOrderNode != NULL )
			return false;
		if( nextPreOrderNode != NULL && nextInOrderNode == NULL )
			return false;
		if( nextPreOrderNode->data != nextInOrderNode->data != NULL )
			return false;
	}while( nextPreOrderNode != NULL && nextInOrderNode =! NULL );
	return true;
}
NODE NextPreOrderSuccessor( NODE &p, _stack &stack, bool mirror/*=false*/ )
{
	if( p != NULL)
	{
		stack.push(p);
		p = (mirror)?p->right:p->left;
		return stack.top();
	}
	
	while( !stack.empty() )
	{
		temp = stack.pop();
		if( ((mirror)?temp-?left:temp->right) != NULL )
			p = mirror?temp->left:temp->right;
			break;
	}
	if( p == NULL || stack.empty() )
		return NULL; // end of traversal
}
NODE NextInOrderSuccessor( NODE &p, _stack &stack, NODE &lastVisited, bool mirror/*=false*/ )
{
	if( p != lastVisited )
		while( p!=NULL )
			stack.push(p);
			p = mirror?p->right:p->left;
	
	if( !stack.empty() ) 
	{	
		temp = stack.pop()
		lastVisited = temp;
		if( mirror?temp->left:temp->right )
			p = mirror?temp->left:temp->right;
		return temp;
	}
	
	if( p == NULL && stack.empty() )
		return NULL; // end of traversal
}

- y2km11 February 26, 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