Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

private static BinaryTreeNode convertToMirror(BinaryTreeNode root) {
		BinaryTreeNode temp;
		if (root != null) {
			convertToMirror(root.getLeft());
			convertToMirror(root.getRight());

			temp = root.getLeft();
			root.setLeft(root.getRight());
			root.setRight(temp);
		}
		return root;
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

A simple recursive solution--

public static TreeNode Mirror(TreeNode root)
{
	if (!root) return null;

	TreeNode node = new TreeNode();
	node.data = root.data;
	node.left = Mirror(root.right);
	node.right = Mirror(root.left);

	return node;
}

- zahidbuet106 December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mirrorTree(TreeNode root){
//
if(root == null)
return;
mirrorTree(root._leftChild);
mirrorTree(root._rightChild);

if(root._leftChild!=null){
root._leftChild._rightChild = root;
root._leftChild = null;
}
if(root._rightChild!=null){
root._rightChild._leftChild = root;
root._rightChild = null;
}
}

- AVK September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one's the right answer!

- AVK September 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

A simple recursive solution

public static TreeNode Mirror(TreeNode root)
{
	if (!root) return null;

	TreeNode node = new TreeNode();
	node.data = root.data;
	node.left = Mirror(root.right);
	node.right = Mirror(root.left);

	return node;
}

- zahidbuet106 December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it OK?

void mirrorCopy(Node *node){
	if(node == NULL) 
		return;
	mirrorCopy(node->m_pLeftChild);
	mirrorCopy(node->m_pRightChild);
	Node *pTemp;
	pTemp = node->m_pLeftChild;
	node->m_pLeftChild = node->m_pRightChild;
	node->m_pRightChild = pTemp;
}

- Riasat Abir October 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void createMirror(Node Root)
{
   if(Root == null)
   return;

   Node L = Root.Left;

   Node R= Root.Right;

   Root.Left=R;
   Root.Right=L;

   CreateMirror(Root.Left);
   CreateMirror(Root.Right);
}

- rohit September 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

oops my bad

public void createMirror(Node Root)
{
   if(Root == null)
   return;

   CreateMirror(Root.Left);
   CreateMirror(Root.Right);

   Node L = Root.Left;

   Node R= Root.Right;

   Root.Left=R;
   Root.Right=L;

}

- Rohit September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

You killed my original tree :_(

- bigphatkdawg September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well both iterations solve the purpose. the only question perhaps i didn't address is perhaps the requirement is to create a new tree instead of making the change in place

- rohit September 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

if both iterations solve the purpose, why did you say "oops my bad" and change it? idiot.

- bigphatkdawg September 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bigphatkdawg - u noob douche

- rohit September 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

you should swap in the post order fashion and not the preorder one.
- Anonymous on September 22, 2013

oops my bad

public void createMirror(Node Root)
<snip, Rohit makes useless change>

- Rohit on September 22, 2013

Rohit = non-noob

- bigphatkdawg September 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bigphatkdawg is a big phat idiot. k?

- row_hit. September 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1 agree

- rohit's mom September 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

you should swap in the post order fashion and not the preorder one.

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

Why should pre/post order

- ug September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why should the order matter?

- ug September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't matter.
{Different strokes for different folks. }

- bigphatkdawg September 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void mirror(Node originalTree, Node newTree)
{
if(originalTree== null)
return;

Node left = OriginalTree -> right;
Node right= OriginalTree -> left;

newTree = (Node) malloc (Node*sizeof(Node));
newTree->value = originalTree->value;
newTree->left = null;
newTree->right = null;

mirror(left, newTree -> left);
mirror(right, newTree -> right);
}

- Nitesh Gupta September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public TreeNode mirrorTree(TreeNode origTree)
{
if(NULL == origTree)
return NULL;

Node node = new TreeNode();
node->value = origTree->value;

node->left = mirrorTree(origTree->left);
node->right = mirrorTree(origTree->right);

return newTree;
}

- AndrewSun September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public TreeNode mirrorTree(TreeNode origTree) 
{ 
	if(NULL == origTree) 
		return NULL; 

	Node node = new TreeNode();
	node->value = origTree->value;
	
	node->left = mirrorTree(origTree->left); 
	node->right = mirrorTree(origTree->right);

	return newTree; 
}

- AndrewSun September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public TreeNode mirrorTree(TreeNode origTree) 
{ 
	if(NULL == origTree) 
		return NULL; 

	Node node = new TreeNode();
	node->value = origTree->value;
	
	node->left = mirrorTree(origTree->left); 
	node->right = mirrorTree(origTree->right);

	return node; 
}

- AndrewSun September 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void TreeOperation::mirrorTree(TreeNode* node){
	if(node != NULL){
		mirrorTree(node->getLeftChild());
		mirrorTree(node->rightChild);
		swapChildren(node);
	}
}

void TreeOperation::swapChildren(TreeNode* parent){
	TreeNode* temp;
	temp=parent->getLeftChild();
	parent->setLeftChild(parent->rightChild);
	parent->rightChild = temp;
}

Please let me know If I am wrong

- Rudra September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

geekyjumps.blogspot.in/2013/09/create-mirror-of-binary-tree.html

- Anonymous September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

geekyjumps.blogspot.in/2013/09/create-mirror-of-binary-tree.html

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

+1 i like for website of the url http's

- rohit's mom September 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

private TreeNode createMirror(TreeNode oldTree, TreeNode newTree){
    if(oldTree == null || newTree == null){return null;}
    newTree.left = oldTree.right;
    newTree.right = oldTree.left;
    createMirror(oldTree.left, newTree.right);
    createMirror(oldTree.right, newTree.left);
}

public TreeNode createMirror(TreeNode oldTree){
  if(oldTree == null){return null;}
  TreeNode newTree = oldTree;
  return createMirror(oldTree, newTree);
}

- Anonymous September 25, 2013 | 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