Amazon Interview Question for SDE1s


Team: Bangalore
Country: India
Interview Type: In-Person




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

public Node doMirror(Node n)
{
 	if(n!=null)
	{
		Node temp = node.right;
		node.right=node.left;
		node.left=temp;
		doMirror(node.left);
		doMirror(node.right);
	}
}
Alternate Row Mirror Assuming complete binary tree

Logic 
     get child row  and childchild row 
	 reverse child row  rearrange the childschild row and call again
	public void AlternateMirror(Noot root)
	{
		if(root!=null)
			{
				List<Node> currentArray= new ArrayList<Node>();
				currentArray.add(root);
				while(currentArray.size()>0)
				{
					List<Node> childArray = new ArrayList<Node>();
					foreach(Node n in currentArray)
					{if(n.left!=null)    childArray.add(n.left);
					   if(n.right!=null)  childArray.add(n.right);
						}
					List<Node> childchildArray = new ArrayList<Node>();
					foreach(Node n in childArray)
					{if(n.left!=null)    childchildArray.add(n.left);
					   if(n.right!=null)  childchildArray.add(n.right);
						}
					Collection.reverse(childArray);
					int i=0;
					int size = childArray.size();
					foreach(Node n in currentArray)
							{
								if(i>size-1)	n.left= childArray.get(i++);
								if(i>size-1)    n.right= childArray.get(i++);
							}
					int i=0;
					int size = childchildArray.size();
					currentArray.empty();
					foreach(Node n in childArray)
							{
								
								if(i>size-1)	n.left= childchildArray.get(i++);
								if(i>size-1)    n.right= childchildArray.get(i++);
							}
					forEach(Node n in childchildArray)
						{
						   if(child.left!=null)currentArray.add(child.left);
						   if(child.right!=null)currentArray.add(child.right);
						}
						childArray.empty();
						childchildArray.empty();
				}
	}

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

seems working for me

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

can career cup stop people from uploading code longer than 10 lines please? Holy cow:) how long did you think about this?

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

Why not to use Stack for the iterative version?

- Avishai August 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

The simple mirroring is easy here is the solution for alternate mirroring

//Initial value for level is 1 i.e root
void alterMirror(Node noot, int level){
	if(root==null)
		return;
	if(level%2==0){
		Node tem = root.left;
		root.right = root.left;
                root.left = temp;
}
level++;
alterMirroe(root.left,level)
alterMirror(root.right,level);
}
		
}

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

Iterative version for same ...................... use one structure to keep track of level and nodes


struct lvlnode {
int lvl;
struct node *no;

};






void mirror1(struct node *t){

if(t==NULL ) return ;

stack<struct lvlnode > s1;
struct lvlnode l;
l.no=t;
l.lvl=1;
s1.push(l);
struct lvlnode t1;
stack<struct lvlnode> s2;
while(!s1.empty()){

t1=s1.top();
s1.pop();
s2.push(t1);

t=t1.no;
if(t->left){
t1.no=t->left;
t1.lvl=t1.lvl+1;
s1.push(t1);
}
if(t->right){
t1.no=t->right;
t1.lvl=t1.lvl+1;
s1.push(t1);

}


}

while(!s2.empty()){

struct node *temp;
t1=s2.top();
s2.pop();
int level=t1.lvl;
struct node *a=t1.no;
if(level %2 ==0){
temp=a->left;
a->left=a->right;
a->right=temp;
}


}
}

- Anonymous March 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Build a array from the traversal of binary tree.Swap the elements present at index 2^i to 2^(i+1)-1 where i is odd.Then construct the tree from the queue.

- Sibendu Dey July 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Will post code solution if the solution seems correct to others..

- Sibendu Dey July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not to implement the iterative version with simple Stack structure?

- Avishai August 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void MirrorAlternateWrapper(Node root){
if( root != null){
mirrorAlternate(root, 0);
}

}

void mirrorAlternate(Node root, int level){
if(root == null) return;

mirrorAlternate(root.left, level+1);
mirrorAlternate(root.right, level+1);

if(level %2 == 0){
Node temp = root.right;
root.right = root.left;
root.left = temp;
}
}

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

//using preorder

struct node *mirror(struct node *root) {

if (root == NULL)
return NULL;
else {

struct node *n = malloc (sizeof(struct node));

n->value = root->value;
n->left = mirror(root->right);
n->right = mirror(root->left);

return n;
}
}

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

Mirroring alternate levels

struct node* mirror_alternate_levels(struct node* tree,int level)
{
        if(tree==NULL)
            return NULL;
        else
        {
            struct node* l=mirror_alternate_levels(tree->left,level+1);
            struct node* r=mirror_alternate_levels(tree->right,level+1);
            if(level%2==0)
            {
                tree->left=r;
                tree->right=l;
            }
            else
            {
                tree->left=l;
                tree->right=r;
            }
        }
}

- pras July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

This one is the code using recursion

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
typedef struct tree tree_t;
struct tree
{
	int data;
	tree_t *left;
	tree_t *right;
};
tree_t *newNode(int data)
{
	tree_t *n=(tree_t *)malloc(sizeof(tree_t));
	n->data=data;
	n->left=n->right=NULL;
	return n;
}

void preorder(tree_t *root)
{
	if(root==NULL)
		return;
	printf(" %d ",root->data);
	preorder(root->left);
	preorder(root->right);
}
void mirror(tree_t *t)
{
    if(t==NULL)
        return;
    else
    {
        tree_t *temp;
        mirror(t->left);
        mirror(t->right);
        temp=t->left;
        t->left=t->right;
        t->right=temp;
    }
}
void mirrorAlt(tree_t *t)
{
	if(t==NULL)
	{
		return;
	}
	else
	{
		tree_t *temp=t->left;
		t->left=t->right;
		t->right=temp;
		if(t->left==NULL&&t->right==NULL)
			return;
		else if(t->left!=NULL&&t->right==NULL)
			mirrorAlt(t->left->left);
		else if(t->right!=NULL&&t->left==NULL)
			mirrorAlt(t->right->right);
		else
			{
				mirrorAlt(t->left->left);
				mirrorAlt(t->right->right);
			}
		return;
	}
}
int main()
{
	tree_t *root=newNode(1);
	root->left=newNode(2);
	root->left->left=newNode(4);
	root->left->right=newNode(5);
	root->left->right->left=newNode(6);
	root->left->right->right=newNode(7);
	root->right=newNode(3);
	printf(" Mirror of the tree preorder\n");
	mirror(root);
	preorder(root);
	printf("\nMirror at alternate levels preorder\n");
	mirrorAlt(root);
	preorder(root);
}

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

I can not understand how is the mirrorAlt function is mirroring at alternate levels

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

Paste the code and print it. You will get the nodes mirrored at alternate levels. Actually i am recursively passing root->left->left to pass one level down from the current node that is alternate levels similarly root->right->right to get the alternate level in the right side. This further extends down to the below node recursively thus accounting for alternate levels at the next node.

- vgeek July 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

void mirror(tree_t *t)
{
if(t==NULL)
return;
else
{
tree_t *temp;
mirror(t->left->left);
mirror(t->right->right);
temp=t->left;
t->left=t->right;
t->right=temp;
}
}

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

At alternating levels it means that the mirror of the tree is done at the alternate levels that is the mirroring property persists generally at the alternate levels that is starting at the root at odd levels whereas it remains as it is at the even levels

- vgeek July 15, 2013 | 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