Amazon Interview Question for Software Engineer in Tests


Country: India
Interview Type: Phone Interview




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

int sumify(Node node){
   if(node == null)
      return 0;
  else{
   int tmp = node.data;
   node.data = sumify(node.left)+sumify(node+right);
   return node.data+tmp;  //corrected this
  }
}

- akshay.shetye November 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)Traverse the tree from root to leaf and store the sum of left and right child in the node.
2)When leaf nodes are reached, set the leaf node values as 0
3)Repeat step 1.

- Puzzle November 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. sum(root)
2. if leaf mark zero and return original value saved in temp
3. else root->value = sum(root->lc) + sum(root->rc);
4. return root->value;

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

easy, a recursive function will do that.

- xiaolong cheng November 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void changenodeVal(node1 p)
{
int leftval=0,rightval=0;
if(p!=null)
{
if(p.left!=null)
{
leftval=p.left.val;
}
if(p.right!=null)
{
rightval=p.right.val;
}
changenodeVal(p.left);
changenodeVal(p.right);

if(p.left!=null&&p.right!=null)
p.val=leftval+rightval+p.left.val+p.right.val;
else
p.val=leftval+rightval;
}
}

- noname December 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi akshay.shetye, i think the code will be like this because when you store it parent node it will update parent own value also.which could not recieved by grandparent. and i think it will update all node of tree with '0' value.

plz correct me if am wrong..

int sum(struct node* root)
{
if(root==null)return 0;
if(root->left==null) root->left->data=0; //correct me here if i am wrong
if(root->right==null) root->right->data=0;

root->data=root->left->data + root->right->data +sum(root->left)+sum(root->right);
return 0;
// i think every node of binary tree has been modified.
}

- time November 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if(root->left==null) root->left->data=0; //correct me here if i am wrong
if(root->right==null) root->right->data=0;

This i wrong.. If root->left is NULL, how can you assign data to NULL node..

Following will work...

int sum(struct node* root)
{
if(root==null)return 0;
if(root->left!=null)
root->data = sum(root->left->data) + root->right->data
else
root->data = 0;
if(root->right!=null)
root->data += sum(root->right->data) + root->right->data
else if(root->left == NULL)
root->data = 0;
else
root->data += 0;
return 0;
}

- bg November 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

//how is it??pls check

int sum(struct node* root)
{
if(root==null)return 0;
int i=root->data;
root->data=root->left->data + root->right->data +sum(root->left)+sum(root->right);
return root->data+i;
}

- abhra November 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi, i corrected my ans and i think it should work fine now

- akshay.shetye November 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hii Akshay..ur code is same as me........Abhra

- abhrajuit November 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@bg i knew that but i did not know wht to do. because i had to give that condition.
by the way you should pass argument ass node pointer in your function ,not value
sum(roo->link->data) //this is wrong,pass here link of node :)

- time November 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Abhra

Your code will have unexpected behavior. C/C++ does not guarantee any ordering of summation, which means the summation can be executed in any order and you can get different answers each time.

- axeman.86 November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I cant understand ur point.pls elaborate..My solution is just a recursive way and addition is done after recursion...where is da issue??

- abhrajuit November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This has been posted before. The keypoint is to label every node with an index from left to right.
void SumFromLeftToRight(TreeNode* root, hash_map<int, int>& sums)
{
if(root == NULL)
return;

stack<TreeNode*> s;
stack<int> sIndex;
TreeNode* current = root;
int currentIndex = 0;

while(current != NULL || !s.empty())
{
if(current != NULL)
{
if(sums.find(currentIndex) == sums.end())
sums[currentIndex] = 0;

sums[currentIndex] += current->value;

s.push(current);
sIndex.push(currentIndex);

--currentIndex;
current = current->left;
}
else
{
current = s.top();
currentIndex = sIndex.top();
s.pop();
sIndex.pop();


if(current->right != NULL)
{
++currentIndex;
}

current = current->right;
}
}
}

- Anonymous November 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

int sumTree(node* root)
{
if(!root)
return 0;
int l = sumTree(root->left);
int r = sumTree(root->right);
int lv=0,rv = 0;
if(root->left)
lv = (root->left)->val;
if(root->right)
rv = (root->right)->val;

root->sum = l + r + lv + rv;

return root->sum;

}

- Ratna Kumar November 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- moltencrown November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey31442" class="run-this">class Sum {
public int childSum(Node root) {
if(root == null) {
return 0;
}
int returnVal = root.getData();
int lsum = sum(root.getLeft());
int rsum = sum(root.getRight());
root.setData(lsum + rsum);
return returnVal;
}
}
</pre><pre title="CodeMonkey31442" input="yes">
</pre>

- forinterview2 November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Note: I think , you have written sum in the place of childSum
Ignoring that,
Idon't think your code would serve the purpose. If you execute your code say for -2 in the example, you would store 4 in place of -2 and return -2, which is wrong. You should return 4+-2 because for root(10 here) we would need the sum of left child which is 2. I think changing the last line from
"return returnVal" to "return returnVal+root.getData()" would work

- Manjunath November 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not write an iterative postorder. In visit instead of printing take the sum of left and right trees. any comments?

- MaineChodaCCKo November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice...cool soln...

- CCBadiMalHaiYarLarTapakRahaLundSe November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, i attended the interview process last week, the same qns were asked... Where was your interview? Banglore or chennai?

- Puzzle November 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

chennai

- abcd November 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Duplicate for qns 11635902

- Puzzle November 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sumify(Node *root)
{
	if (root == NULL)
	{
		root->sum = root->data;
	}
	else
	{
		root->sum = sumify(root->left) + sumify(root->right) + root->data;
	}
	return root->sum;
}

- ACP Pradyuman November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int sumify(Node *root)
{
	if (root == NULL)
	{
		return 0;
	}
	else if (root->left == NULL && root->right == NULL)
	{
		root->sum = root->data;
	}
	else
	{
		root->sum = sumify(root->left) + sumify(root->right) + root->data;
	}
	return root->sum;
}

- ACP Pradyuman November 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int
cal_sum(struct node *root)
{

int l, r;

if(root==NULL)
return 0;

int t= cal_sum(root->l) + cal_sum(root->r);

printf(" %d", t);
return t+(root->val);
}

- raja roy November 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple way to do this is, with out modifying the tree itself.

print_sum(node *root) {

if (root->left == NULL && root->right->==NULL){
cout<< 0;
sum = root->item;
}
else {
sum = print_sum(root->left)+ print_sum(root->right);
cout << sum;
}
return (sum);

}

- Himanshu November 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A post-fix traversal (left, right, root) should work.
root would contain the sum of left + right.
Is there anything more complexity involves that I'm missing.

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

static void findSum(int n){
		if (a1[(2*n)] == 0 || a1[(2*n)+1] == 0 ){
			return ;
		}
		else
		{
			findSum(2*n);
			findSum(2*n+1);
			
			
			b1[n] = a1[2*n] + a1[2*n+1];
			int temp = a1[n];
			a1[n] = a1[2*n] + b1 [2*n] + a1 [2*n + 1] + b1 [2*n + 1];
			b1[n] = temp ;
			
		}	
	}

given a1 array with index starting from 1, call findSum(1) . Here terminating condition i have assumed the child of leaf node has value 0 . It will return everything corect except for leaf nodes whose value would be 0 in all the cases

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

In the above solution , b1 is empty array with all the values 0

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

int sumsubtree(node* root, int prev)
{
if(root==NULL)
{
return 0;
}
else
{
int temp, sum;
temp= root->data;
root->data = sumsubtree(root->left, temp) + sumsubtree(root->right, temp);
return root->data + temp;
}
}

- Laxmi December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void changenodeVal(node1 p)
{
int leftval=0,rightval=0;
if(p!=null)
{
if(p.left!=null)
{
leftval=p.left.val;
}
if(p.right!=null)
{
rightval=p.right.val;
}
changenodeVal(p.left);
changenodeVal(p.right);

if(p.left!=null&&p.right!=null)
p.val=leftval+rightval+p.left.val+p.right.val;
else
p.val=leftval+rightval;
}
}

- noname December 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int NodeVal(struct Node *node)
{
if(node == NULL)
return 0;
else
{
int lval=0;
int rval=0;
if(node->left != NULL) lval = node->left->data;
if(node->right != NULL) rval = node->right->data;
int sum = NodeVal(node->left)+NodeVal(node->right);
node->data = sum + lval + rval;
}
}
}

- bhavin December 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if we do postorder traversal....and add children...as we move up

- trojansmith December 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void sumnodes(tree *temp)
{
	if(temp)
	{
		if(temp->left)sumnodes(temp->left);
		if(temp->right)sumnodes(temp->right);
		if(temp->left && temp->right)temp->sum=temp->left->sum + temp->right->sum + temp->left->data + temp->right->data;
		else if(!temp->left && !temp->right)temp->sum=0;
	}
}

- trojansmith December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int getSum(struct node* root)
{
if(root == NULL)return 0;
int lSum=getSum(root->left);
int rSum=getSum(root->right);
int curVal=root->data;
root->data=lSum+rSum;
return lSum+rSum+curVal;
}

- Amritanshu December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote
Check this solution {{{ int sumNode(Node* node) { // node->value is the integer assigned to the node // node->sum is the sum held by the node int leftVal = 0; int rightVal = 0; if(node == NULL) return 0; leftVal = sumNode(node->left); rightVal = sumNode(node->right); node->sum = leftVal + rightVal; return (node->sum + node->value) } - axeman.86 November 22, 2011 | 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