Walmart Labs Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

All the parent-child relations would remain same except those who are parents of that leaf node .i.e, the parent of the leaf node , its parent , and so on till the root . Their relationship will be reversed . The parent will become child and vice versa .
To get the modified tree , just reverse that one chain of leaf's parents . It can be done easily by swapping the leaf with the root , leaf parent with the root child and so on .
A diagram could explain more properly . Hope this helps .

- praveenkcs28 January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how will you implement the reverse part'

- geekykid January 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for the implementation part , just recusrilevy find all ancestors of the leaf in bottom up manner using resursion , and at each get just reverse the link with its parent.
Implementation is easy itry it out , else I'll post

- Geek January 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the path reversal, from the first child to the leaf node, keep on rotating the tree around that particular node accordingly. If it is left child then rotate right else rotate left as we do in AVL tree.

- bansalnvn January 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sir, Please post the code of this problem

- amit January 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Following a version that also maintains a nodes parent:

{
	public Node upsideDownTreeRercursive(Node node, Node newRoot) {
		Node oldParent = null;
		
		if (node == null)
			return null;
		
		if (node == newRoot) {
			node.setLeftChild(node.getParent());
			node.setParent(null);
			return node;
		}
		
		if (upsideDownTreeRercursive(node.getLeftChild(), newRoot) != null) {
			oldParent = node.getParent();
			node.setParent(node.getLeftChild());
			node.setLeftChild(oldParent);
			return node;
		}
		
		if (upsideDownTreeRercursive(node.getRightChild(), newRoot) != null) {
			oldParent = node.getParent();
			node.setParent(node.getRightChild());
			node.setRightChild(oldParent);
			return node;	
		}
		
		return null;
	}

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

thnx

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

Having a parent pointer is not very common. Try without it.

- NJC August 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

CAn you be more specific as to what do you mean by falling down? An example would be great.

- Rage January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you explain with Example means before and after result.

- kamal January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

given tree.
......

1
              /  \
            2   3
           /  \    \ 
         4   5   6
let the leaf node be 5.
then it becomes something like 
                        5
                          \
                          2
                         /  \
                       1   4
                      /
                    3
                   /
                 6

- geekykid January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Was this the example given by the interviewer ?
because I think when the tree falls down holding the leaf node it should look like for your given input:
5
/
2
/ \
4 1
\
3
\
6

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

sorry I am not able to draw the correct picture.
what I want to draw is written below :
root=5
root->left=2
root->left->left=4
root->left->right=1
root->left->right->right=3
root->left->right->right->right=6
correct me please if I have understood something wrong.

- Anonymous January 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#perl syntax
my $n=$designated_leaf;

my $ret=flip_tree($root,null,$left);
if($ret!=0){
        $ret=flip_tree($root,null,$right);
}

sub flip_tree{

        my ($r,$p,$side)=(shift,shift,shift);
        my $ret=0;
        if ($r==$n){
                $ret=1;
        }
        if($node_found==0){
                $ret=flip_tree($node->left, $r, $left);
        }
        if($node_found==0){
                $ret=flip_tree($node->right,$r, $right);
        }

        if($ret==1){
                #swap parent and $r

                if ($side==left){
                        $r->left=$p;

                }else{
                        $r->right=$p;
                }
        }
        return $ret;

}

- Wahid January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Corrections below. Can someone delete this post.

- Wahid January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#perl syntax 
my $n=$designated_leaf;

my $ret=flip_tree($root,null,$left);
if($ret!=0){
        $ret=flip_tree($root,null,$right);
}

sub flip_tree{

        my ($r,$p,$side)=(shift,shift,shift);
        my $ret=0;
        if($r==null) return 0;
        if ($r==$n){
                $ret=1;
        }
        if($ret==0){
                $ret=flip_tree($node->left, $r, $left);
        }

        if($ret==0){
                $ret=flip_tree($node->right,$r, $right);
        }

        if($ret==1){
                #swap parent and $r

                if ($side==left){
                        $r->left=$p;

                }else{
                        $r->right=$p;
                }
        }
        return $ret;

}

- Wahid January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node findNewRoot(Node root, Node newRoot)
{
	if(root != null && newRoot != null)
	{
		if(root.ele == newRoot.ele)
		{
			return root;
		}
		Node R ;
		if(rooot.ele < newRoot.ele)
		{
			R = findNewRoot(root.right, newRoot);
			root.right = null;		
		}
		else
		{
			R = findNewRoot(root.left, newRoot)
			root.left = null;
		}
		R.insert(root);
		return R;
	}
	return null;
}

- alexlogan25 February 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Start from the root, search only the left branch, for each node in that search, change the left child of that node to the parent, the right child to be the left child.

- kotiya February 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

until reach the new root

- kotiya February 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

unless the new root is the right child of some node, then for that node, put the old parent to be the right child, left chid as the left child, and right child to be the parent

- kotiya February 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a graph and do bfs traversal thus getting predecessor array

- prashant July 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a graph and do bfs traversal thus getting predecessor array

- prashant July 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Node* flipWrapper(Node* root, Node* leaf) {
	Node* retVal = flip(root, leaf);
	if(retVal) {
		return leaf;	// new root
	}
	
	return root;	// could not find target leaf. So return original root
}

Node* flip(Node* root, Node* target) {
	if(root == NULL) 
		return NULL;

	if(root == target && root is leaf) {
		return root;
	}

	Node* L= flip(root->left, target);
	if(L) {
		root -> left = NULL;
		L->right = root;
		return root;
	}

	Node* R= flip(root->right, target);
	if(R) {
		root->right = NULL;
		R->left = root;
		return root;
	}

	return NULL;
}

- Saumesh April 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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