## iLabs Interview Question for Tech Leads

Country: India
Interview Type: In-Person

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

Do a reverse inorder keeping a cumulative sum of the elements seen so far.

``````void ReplaceGreaterSum(Tree *node, long *sum) {
if (node == NULL) return;
if (node->Right) ReplaceGreaterSum(node->Right, sum);
*sum = *sum + node->Value;
node->Value = *sum;
if (node->Left) ReplaceGreateSum(node->Left, sum);
}

void ReplaceGreaterSum(Tree *root) {
long sum = 0;
ReplaceGreaterSum(root, &sum);
}``````

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

Good one. Another alternative with similar effect is to create a 'reflected' BST where min node will occur at the place of max node etc. Then do an inorder traversal, keeping the running cumulative sum and updating the node values and then 'reflect' the BST back.

Doing a reverse inorder is more efficient both in terms of time and space though.

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

``````public int  replaceNodeWithRightSubtree(Node node){
if(node==null)return 0;
int rightSum=0;
if(node.right!=null){

rightSum=replaceNodeWithRightSubtree(node.right);
}
node.value=node.value+rightSum;
if(node.left!=null){
node.left.value=node.left.value+node.value;
replaceNodeWithRightSubtree(node.left);
}

return node.value;

}``````

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

You have no friends to bounce the solution onto so you are a loner.

- Guest _BS_

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

You are funny.. ;)

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

@Luri

You are right. Shukla has no friends. He only has got a father that is me.

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

Oye Shukla ke bachche... abhi tak kahan kahan app maara hain aur kahan lagi hain job?

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.

### 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.