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);
}

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

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.

- Murali Mohan November 26, 2013 | Flag
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;
		
	}

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

You posting question and answer so other can verify your sol?

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

- Guest _BS_

- Luri Covalisin (aka the real algos) November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are funny.. ;)

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

@Luri

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

Shukla's Dad

- Shukla's father November 26, 2013 | Flag
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?

- Shukla ka baap November 26, 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