Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
7
of 9 vote

Tested for all edge cases -

public int sumOfAllGreater(TreeNode root, int i){
		if(root == null){
			return i;
		}
		else if(root.leftChild == null && root.rightChild == null){
			root.data += i;
			return root.data;
		}
		
		int tmp = sumOfAllGreater(root.rightChild, i);
		root.data += tmp;
		tmp = sumOfAllGreater(root.leftChild, root.data);
		return tmp;
	}

Should call sumOfAllGreater(bst.root, 0) initially..

- srirangr May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think this works correctly. Try it on the following BST ;

7
                        \ 
                        9
                      /   \
                    8   10 


it generates:

                       34
                        \ 
                        19
                      /   \
                    27    10

the 27 is wrong. it has to remain 8

- pixelseeker May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the nodes with values 9 and 10 are greater than 8. So they are also added to 8. Hence it should be 27 only.

- srirangr May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops. saw that mistake. yes you are correct.

- pixelseeker May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int sumNode(Node root, int sumTillNow)
{
int rightSum = 0;
if (root.right != null)
{
rightSum = sumNode(root.right, sumTillNow);
}
int rootValBackup = root.val;
root.val = (sumTillNow + rightSum);
sumTillNow = rootValBackup + root.val;
if (root.left != null)
{
sumTillNow += sumNode(root.left, sumTillNow);
}
return sumTillNow;
}

- Venkatesh July 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int sumNode(Node root, int sumTillNow)
{
    int rightSum = 0;
    if (root.right != null)
    {
        rightSum = sumNode(root.right, sumTillNow);
    }
    int rootValBackup = root.val;
    root.val = (sumTillNow + rightSum);
    sumTillNow = rootValBackup + root.val;
    if (root.left != null)
    {
        sumTillNow += sumNode(root.left, sumTillNow);
    }
    return sumTillNow;
}

- Venkatesh July 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
4
of 6 vote

This means that each node will contain its data + sum of all node's data in right subtree.
node->data = node->data + (sum of all nodes in right subtree).
Reverse Inorder would do the work for us.

Code:
int sum = 0; //global variable
sum_max (node *root)
{
if (root == NULL)
return;

sum_max (root->right);
sum = sum + root->data;
root->data = sum;
sum_max (root->left);
}

- Punit Jain May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes

Stop upvoting yourself. THere are at least two answers, which are better formatted. The answer by avondale is especially succint.

- Anonymous May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The code is correct and produces right output.
What is the problem in using global variable ? or you are unfamiliar with them ?

- Punit Jain May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if a node is left child of its parent ,it will be having its right subtree sum and its parent and parents right subtree also.it wont be only right subtree sum

- Anonymous May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry,i misunderstood.

- Anonymous May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Punit: If you need to ask why using globals could be bad, then you need to do some more reading.

- first_anonymous May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

1. Postorder travarsal.
     2. currnode --> data += prevNode-->data

}

- kg May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

// Trying to write the code, only bad part is the global variable, in    
//Java though we can use as private variable what I have tried here.
private Node prevNode = null;
public void PostOrderSum( Node node ) {
          if( node == null ) return;

          PostOrderSum( node.right );
          PostOrderSum( node.left );
          
          if( prevNode  != null ) {
                     node.data += prevNode.data;
           }

          prevNode = node;
          return;
}

}

- kg May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Should be reversed inorder not postorder.

- Anonymous May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. Reverse inorder traversal will do.

- Arun May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

true
reverse inorder will do the job.

- soumyajuit May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

void newBST(TreeNode node,int sum){
        if(node==null) return;
        newBST(node.right,sum);
        sum=sum+node.data;
        node.data=sum;
        newBST(node.left,sum);

}

- Anonymous May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

agreed, the solution is reverse inorder.

- kg May 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

int fun(struct node *right)
{
    if(!root->right)
         return root->data;
    root->data+=fun(root->right);
    return (root->data +fun(root->left));
}

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

If we have performed the requisite algorithm (rollUp) on the right subtree of a node, and now we need to perform that on the current node, we can just consider the value of the right node.

But the issue here is that the right node had ignored it's left subtree for getting it's value(because at any node, all the nodes in the right subtree have value greater than it.)

But this neglected left subtree of the current node's right node is important for calculating the value of current node.

So what we can do is.. we can try to remember the left subtree's sum, at any node of the tree, while doing recursion.

- sakshi August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So, here is my code..
done dry testing only :-)

int roll(struct node *root)
{
   if(root == NULL) return 0;
   int l_sum=0, r-sum=0;
   l_sum = roll(root->left);
   r_sum = roll(root->right);
   root.data = root.data + root->right.data + r_sum;
   return (node->left.data + l_sum);
}

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

Do a Reverse Inorder. First Right subtree, then root, then Left.

(Postorder is not right as root node will get sum of all its children!)

Probably something like this (note: not tested)

void Summer(const Tree *t) {
    int so_far = 0;
    SummerInternal(t, so_far);
}
 
void SummerInternal(const Tree *t, int &so_far /*input, output*/) {
    if (t == NULL) return;
    
    Summer(t->right, so_far);
    
    int tmp = t->value;
    t->value += so_far;
    so_far += tmp;
    
    Summer(t->left, so_far);
}

- Anonymous May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

I guess it is the "flipped" in-order traversal, which is different from post-order.

// "func" returns ("a" + sum),
// sum is equal to the sum of all the original elements in BST rooted at "r".
int func(Node r, int a) {
    if (r == null) return a;
    r.data += func(r.right, a);
    return func(r.left, r.data);
}

- avondale May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

virtual virtual! +1. (I don't want to register here).

Neat code!

- Anonymous May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

each node has value equal to sum of all the nodes (including itself) which are greater than that node in the whole tree.not sum of alll node...so i think solut

- Anonymous May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is int a requiered as an argument?

the value assigned to it is r.data which is also available while it is getting returned: Instead of a we could return r.data itself

int func(Node r) {
if (r == null) return r.data;
r.data += func(r.right);
return func(r.left);
}

its more readable now

- Mike May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Mike, NPE in ur code in case of r == null

- npe May 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hmm ya

- Mike May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if a node is left child of its parent ,it will be having its right subtree sum and its parent and parents right subtree also

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

int inorder(struct node* root,int sum)
{
static int sum1=0;
if(root != NULL)
{
inorder(root->right,sum1);
sum1+=root->data;
root->data= sum1;
printf("%d,",root->data);
inorder(root->left,sum1);

return root->data;
}

else
return 0;

}

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

really superb.......understood..thanks guy...

- mca June 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int inorder(struct node* root,int sum)
{
static int sum1=0;
if(root != NULL)
{
inorder(root->right,sum1);
sum1+=root->data;
root->data= sum1;
printf("%d,",root->data);
inorder(root->left,sum1);

return root->data;
}

else
return 0;

}

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

xactly correct code dude!!!

- saurabh May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong soln.

- code_guru June 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

addRight(NODEPTR node)
{
   if(node==NULL) return 0;
   if(node->right==NULL & node->left==NULL) //leaf node
        return node->info;
   node->info=node->info+addRight(node->right);
   return node->info+addRight(node->left);
}

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

Every time the function should return the sum and this sum must be passed to every recursive call. We are thereby passing the maximum value of all the nodes to call and reverse inorder traversal does the trick.

int Func(Node* root,int sum){
int rsum,lsum;
if(root==NULL)return sum;

rsum = func(root->right,sum);
root->data= rsum+root->data;
rsum=root->data;
printf("\t%d",root->data);
lsum=func(root->left,root->data);
if(rsum>lsum)sum = rsum;
else sum =lsum;
return sum;
}

- VC May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Friends , can you please explain me how it differs weather i use reverse inorder traversal or post order traversal as long as right traversal is before the current node evaluaiton. What i mean is


public void Traverser(Object node){

if(node.right != null) Traverser(node.right);

node.value+ = node.right.value;

if(node.left !=null ) Traverse(node.left);


}


Or

public void Traverser(Object node){

if(node.right != null) Traverser(node.right);

if(node.left !=null ) Traverse(node.left);

node.value+ = node.right.value;

}




Or

public void Traverser(Object node){


if(node.left !=null ) Traverse(node.left);

if(node.right != null) Traverser(node.right);

node.value+ = node.right.value;



}


all 3 functions should return me the same result is it not ? what am i missing ? please help

- lohith May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry i could not understand your que.. greater than all nodes means value wise greater all node or.. anything else.can you please tell more detail.

- time May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sumGreaterNodes( Node node , int parentValue){
if(node==null){
return 0;
}
int sumNodesRight = sumGreaterNodes(node.right, parentValue);
int temp = node.value;
node.value += (sumNodesRight + parentValue);
int sumNodesLeft = sumGreaterNodes(node.left, node.value);
return temp + sumNodesRight + sumNodesLeft;
}

- random May 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this tree is then no longer a BST.. Should that be done as well ?

- dilip.rangarajan May 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n^2)

Algorithm

Go to each node and calculate right subtree sum. Top Down approach. 2 Recursive calls. Java code follows:

private static void goToEachNodeRecCall(TreeEntry node) {
		
		if (node == null)
			return ;
		
		node.data = node.data + calculateSums(node.right);
		
		goToEachNodeRecCall(node.left);
		goToEachNodeRecCall(node.right);
	}
	
	private static int calculateSums(TreeEntry node) {
		
		if (node == null)
			return 0;
		
		int sum = node.data;
		
		int left = calculateSums(node.left);
		int right = calculateSums(node.right);
		
		sum = sum + left + right;
		
		return sum;
	}

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

What happens if the predecessor has the same value as the current node? Do you count it as a sum or replica? I think what may be best is if in addition to returning sum from recursive call, one also returns a predecessor having greater key.

- Ashish Kaila May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Process right subtree and then left subtree

int sum_BST(Node* iter)
{
if(iter==NULL) return 0;
iter->data = iter->data + sum_inorder(iter->rc);
return sum_inorder(iter->lc) + iter->data;
}

- aejeet May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum_BST(Node* iter)
{
if(iter==NULL) return 0;
iter->data = iter->data + sum_inorder(iter->rc);
return sum_inorder(iter->lc) + iter->data;
}

- aejeet May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int SumofRightTree(Tree *tree, bool isRight)
{
if(tree != NULL)
{
SumofRightTree(tree->left, false);
tree->data = tree->data + SumofRightTree(tree->right, true);
return tree->data;
}
else
return 0;

}
int _tmain(int argc, _TCHAR* argv[])
{
int arr[] = {30,10,5,20,50,40,60};
Tree *root= NULL;

for(int i=0;i<7; i++)
root = CreateTree(root, arr[i]);

SumofRightTree(root, true);
return 0;
}

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

int SumofRightTree(Tree *tree, bool isRight)
{
if(tree != NULL)
{
SumofRightTree(tree->left, false);
tree->data = tree->data + SumofRightTree(tree->right, true);
return tree->data;
}
else
return 0;

}
int _tmain(int argc, _TCHAR* argv[])
{
int arr[] = {30,10,5,20,50,40,60};
Tree *root= NULL;

for(int i=0;i<7; i++)
root = CreateTree(root, arr[i]);

SumofRightTree(root, true);
return 0;
}

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

int sum(NODE *T)
{
if (T==NULL)return 0;

sum(T->left); //dont use return val
int temp = T->data;
T->data = temp + sum(T->right);
return(temp);

}

Please suggest mistake in the code if any.

- code_guru June 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please ignore,there is a problem in code.

- code_guru June 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int convert(TREE root)
{
    int sumval = 0;
	if(root==NULL)return 0;
	convert(root->left);
	convert(root->right);
	if(root->left!=NULL || root->right!=NULL)
	{       //        cout<<"\n poitning to "<<root->info;
	sum(root->left,root->info,&sumval); //important cz though it is a BST initially still it is possible that the left child becomes greater than root intdelf
	sum(root->right,root->info,&sumval);
    root->info += sumval;
	//cout<<root->info;
	sumval = 0;
   }
}
int sum(TREE root,int val,int *sumval)
{
	//int sumval = 0;
	if(root==NULL) return 0;
	if(root->info > val)
	{
    //cout<<"\n Found greater = "<<root->info<<" than "<<val; 
     (*sumval)+=root->info;
     //cout<<"\n sumval = "<<*sumval;
     sum(root->left,val,sumval);
     sum(root->right,val,sumval);
     
     }

}

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

int transform(struct Node* root, int sum) {
  if (!root) return sum;

  root->data += transform(root->right, sum);
  return transform(root->left, root->data);
}

- Pavel September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As most of the people said...keep adding the right children to the curr node while doing rev inorder.Only gotcha was returning sum back when we hit null.This is because, when we hit null, we are essentially returning from right hand side back to parent node always.Even in the case , when we return from left side of child node back to childs parent.

private static int fn2(Node root,int sum) {
        if(root==null) return sum;
        int right=fn2(root.right,sum);
        root.data=root.data+right;
        return fn2(root.left,root.data);
    }

- PM April 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sum(tree*root)
{
static int sum ;
if(!root) return ;
sum(root->right) ;
root.data=sum+root.data ;
sum=root.data ;
sum(root.left) ;

- abhishek July 24, 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