Walmart Labs Interview Question for Software Engineer / Developers


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




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

BST * siblingsTransform(BST *root)
{
if(root == NULL)
return NULL;
siblingsTransform(root->left);
siblingsTransform(root->right);
if(root->left !=NULL)
{
root->left ->right = root->right;
root->right = NULL;
}
else
{
root->left = root->right;
}
return root;
}

- kushalbafna November 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

kushal in the else case too root->right = Null

- Tanmay November 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even if root->right is NULL.. it ll work.. NULL value ll be assigned to root->left tht is same as tht of not assigning it(No Error).

- kushalbafna November 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@kushalbafna, you still have to assign parent's right pointer to null in the else part..that's what Tanmay had mentioned..

- digitalrahul August 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

private static Tree<Integer> convertLeftChildRightSiblingTree(
			Tree<Integer> tree) {
		// TODO Auto-generated method stub
		if(tree==null)
			return null;
		convertLeftChildRightSiblingTree(tree.left);
		convertLeftChildRightSiblingTree(tree.right);
		if(tree.left!=null){
			tree.left.right=tree.right;
		}
		else{
			tree.left=tree.right;
		}
		tree.right=null;
		return tree;
	}

- digitalrahul August 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

So basically you get to the right child by following the right pointer (sibling) of the left child? (or vice versa). Can you please explain more clearly?

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

Yes.

- gowri August 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

isnt that similar to printing the tree level wise? am i missing something here?

- Saurabh August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nope......you should transform in such a way that left child of a node should point to its sibling (i.e. right child) and parent node's right should be null. You should not reconstruct the tree but make use of the current node structure.

- Gowri Shankar August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey Gowri,
Can you explain the question with an example?

- MM August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

LC-RS Tree : en.wikipedia.org/wiki/Left-child_right-sibling_binary_tree

- Navneet Goel October 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using PostOrder Traversal
Complexity : o(N)

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

//assuming that every node has one left child, in case it has only one child

Convert(Node *head){
if(head->left == NULL)
return;
Convert(head->left);
(head->left)->right = Convert(head->right);
head->right = NULL;
}

- J November 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void transform(node *root){
node *p = root->left;
node *q = root->right;
if(p) transform(p);
if(q) transform(q);
if(p) p->right = q;
else root->left = q;
root->right = NULL;
}

- nandan.keshav55 November 09, 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