Amazon Interview Question for Software Engineer / Developers






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

Do simple recursive call to sum up all nodes

int ProcessTree(Node *node)
{
if (!node) return 0;
int cur_node_data = node->data;
node->data = ProcessTree(node->left) + ProcessTree(node->right);
return node->data + cur_node_data;
}

- andrew July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will work fine!!!

- rishi t October 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I've seen the remark, "+ve & -ve numbers," in other postings. I'm unfamiliar with this notation. I assume the questioner intends to mean "signed integers." If that's true, why the reference to +ve and -ve? Why not just say signed integers or even just integers which is the more common usage?

Thanks to anyone who responds.

- Developer July 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Try telling that to the interviewer.

- Mad August 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Method signature - public void ProcessTree(ref TreeNode a)

Algo:
Return if node is null or node's left and right both are null.
Recursive call with node's left pointer
Recursive call with node's right pointer

Assign node's data with sum of left n right

- sandy July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just do the postorder traversal of tree and in each recursive call check weather the parent have same valeue as the sum of its childrean else just do maintain it :)

void BinaryTreeSum(struct node *T)
{
if(!T||T->left==NULL&&T->right==NULL)
return;
BinaryTreeSum(T->left);
BinaryTreeSum(T->right);
if(T->left&&T->right)
sum=T->left->data+T->right->data;
else if(T->left&&!T->right)
sum=T->left->data;
else
sum=T-right->data;
T->data=sum
}

- geeks July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution buddy

- ACP Pradyuman September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

instead of returning void, return 0 when it is null, then we don't need extra checks at the last for the valid pointers

- ajay October 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

THe question isn't very clear.
What is the input to the program? Is it a sequence of signed integers from which we have to make a binary tree with above mentioned rule?

- Akash July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I m assuming there is a sum variable in every node element.

sum( node )
{
if( !node->left && !node->right )
{
node->sum = 0;
return node->val;
}
cumsum = sum(node->left) + sum(node->right);
node->sum = cumsum;
return (cumsum + node->val);
}

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

store previous sum of parent node & keep calling for left & right subtree
replace sum of parent node with sum of left + right subtree

#include <stdio.h>
#include <stdlib.h>

/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};

/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

struct node* sumSubTree(struct node** root)
{
int LeftOld,RightOld;
struct node *l,*r;
if((*root) == NULL || ((*root) -> left== NULL && (*root)->right == NULL))
return NULL;
else
{
LeftOld = (*root)->left->data;
RightOld = (*root)->right->data;
l = sumSubTree(&(*root )->left);
r = sumSubTree(&(*root)->right);
(*root)->data = LeftOld + RightOld;
if(!l)
(*root)->data += l->data;
if(!r)
(*root)->data += r->data;
return *root;
}

}

/* Given a binary tree, print its nodes in inorder*/
void printPreorder(struct node* node)
{
if (node == NULL)
return;

/* first print data of node */
printf("%d ", node->data);

/* then recur on left sutree */
printPreorder(node->left);

/* now recur on right subtree */
printPreorder(node->right);
}

int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);

root=sumSubTree(&root);

printf("\n Preorder traversal of binary tree is \n");
printPreorder(root);

getchar();
return 0;
}

- WgpShashank August 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you take care to put your code in

for better readability. Please follow this for all your responses

- Anonymous August 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please paste only the algorithm? I guess algo is more than enuf

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

A binary tree is given ... how each node is represented is mentioned but wat is too be done isnt clear in the given question... please elaborate the question

- shruti.gautam.dce August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey75851" class="run-this">void ConvertToSumTree(TreeNode* root)
{
if(root != NULL)
{
ConvertToSumTree(root->left);
ConvertToSumTree(root->right);
if(root->left || root->right)
root->data = (root->left ? root->left->data : 0) + (root->right ? root->right->data : 0);
}
}</pre><pre title="CodeMonkey75851" input="yes">
</pre>

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

public void binarySumTree2(Node localRoot){		
		if(localRoot.leftChild!=null&&localRoot.rightChild!=null){
			localRoot.key=getSum(localRoot.leftChild)+getSum(localRoot.rightChild);
			binarySumTree(localRoot.leftChild);
			binarySumTree(localRoot.rightChild);
		}
		if(localRoot.leftChild!=null&&localRoot.rightChild==null){
			localRoot.key=getSum(localRoot.leftChild);
			binarySumTree(localRoot.leftChild);
		}
		if(localRoot.leftChild==null&&localRoot.rightChild!=null){
			localRoot.key=getSum(localRoot.rightChild);
			binarySumTree(localRoot.rightChild);
		}		
	}
	public int getSum(Node localRoot){
		if(localRoot==null){
			return 0;
		}			
		else{
			return localRoot.key+getSum(localRoot.leftChild)+getSum(localRoot.rightChild);
		}			
	}

- akash.kotadiya2000@gmail.com September 12, 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