Amazon Interview Question for Software Engineer / Developers






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

u must be joking,or he must be joking.I don't see how is this correct if complete.

- aditya June 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ya this makes it O(n2).Doing a simple preorder and calculating return value for each node if it exists.I guess there can be more efficient way rather than checking each node even if it will have a sibbling by the property of a "binary tree" ,as we can break it down into cases:
1.The tree is skew.
2.Only leaf nodes to be considered who have no siblings.
3.The subtree is skew,in that case all except the root of subtree.

Though doing something is better than nothing and that too in 15 mins.I hope somebody will surely come up with a cooler solution.

- kapil June 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Incomplete solution.

- Somebody June 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I appreciate your response, it will be great help to people like me who are attending interviews if you provide complete solution instead.

- sk June 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Code will work as soon as tree traversed and complexity is O(n).

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

<pre lang="" line="1" title="CodeMonkey89970" class="run-this">int main()
{
int sum = traverseNode(rootNode, 0);
}
int traverseNode(Node x, sum)
{
if(x.left != null)
{
sum += traverseNode(x.left);
}
if(x.right != null)
{
sum += traverseNode(x.right);
}
if(x.left == null && x.right == null)
{
sum += x.value;
}
return sum;
}</pre><pre title="CodeMonkey89970" input="yes">
I gave it shot.</pre>

- Anonymous June 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you for the code, I have the main function and where I will traverse the tree, I only pasted the logic which Interviewer interested in.

Thanks again.

- sk June 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Code is wrong. It calculates the sum of leaf nodes.

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

i think the above soul wont work, hes finding the sum of nodes with no leafs but thats not the question is r8??

- krishna June 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum = 0;
void nosiblsum(node* n)
{
if(!n || (n->left==NULL && n->right==NULL)
return ;
if(n->left == NULL && n->right!= NULL)
{
sum += n->right->data;
nosiblsum(n->right);
}
else if(n->right == NULL && n->left != NULL)
{
sum += n->right->data;
nosiblsum(n->left);
}
else
{
nosiblsum(n->left);
nosiblsum(n->right);
}
}

- krishna June 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

looks fine!!

- chandan prakash July 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

looks fine!!

- chandan prakash July 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a BFS here is the pseudo code.

1. Enque(Root), sum = 0
2. While Q is not empty repeat 3  to 11.
3.     r=Dqueue();
4.     if(!(r->lClild && r->rChild))
5.         if(r->lChild) 
6.              sum = sum + r->Child.data;
7.         else if (r->rChild) 
8.              sum = sum + r->rChild.data;
9.     if(r->lChild) Enqueue(r-rChild);
10.    if(r->rChild) Enqueue(r->lChild)   
11. Go to 2.
12. Return Sum

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

public static int nodesWithNoSiblings(TreeNode node) {
if (node == null) {
return 0;
}
int leftChildrenWithNoSiblings = nodesWithNoSiblings(node.getLeft());
int rightChildrenWithNoSiblings = nodesWithNoSiblings(node.getRight());
int currentCount = (node.getLeft()==null?1:0) + (node.getRight()==null?1:0);
return currentCount + leftChildrenWithNoSiblings + rightChildrenWithNoSiblings;
}

- Teja June 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Anonymous
Can you pl explain the lines 9 and 10 ? I do not understand that part.

Thanks
S

- Supraja June 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its a typo mistake
below is the corrected:

1. Enque(Root), sum = 0
2. While Q is not empty repeat 3  to 11.
3.     r=Dqueue();
4.     if(!(r->lClild && r->rChild))
5.         if(r->lChild) 
6.              sum = sum + r->lChild.data;
7.         else if (r->rChild) 
8.              sum = sum + r->rChild.data;
9.     if(r->lChild) Enqueue(r-lChild);
10.    if(r->rChild) Enqueue(r->rChild)   
11. Go to 2.
12. Return Sum

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

Resursive solution

int CheckSumOfNoSiblings(Node node) {
    if (node->left == null && node->right != null) 
        return CheckSumOfNoSiblings(node->right) + 1;
    if (node->right == null && node->left != null)
        return CheckSumOfNoSiblings(node->left) + 1;
    if (node->left != null && node->right != null)
        return CheckSumOfNoSiblings(node->left) + CheckSumOfNoSiblings(node->right);
    return 0;
}

- china.taoqin June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

bhakk.... 0
( )
/ \-0
( )
< /

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

int sumUpNodesWOSiblings(tnode* aRoot)
{
static int res;
if(aRoot)
{
sumUpNodesWOSiblings(aRoot->m_left);
if(aRoot->m_left && !aRoot->m_right)
res+=aRoot->m_left->m_data;
if(!aRoot->m_left && aRoot->m_right)
res+=aRoot->m_right->m_data;
sumUpNodesWOSiblings(aRoot->m_right);
}
return res;
};

- hacker June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are not counting the head of the tree which has no siblings.

- Anna June 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sumNotsibling(struct node *r)
{int sum=0;
if(!r)
return;
if(r->left && !r->right )
sum++;
if(r->left && !r->left)
sum++;
sum=sum+sumNotsiblings(r->left)+sumNotsiblings(r->right);
return sum;
}





}

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

public int sumOfSib(Node n,int sum){

if(n==null){
return 0;
}
int lsum = sumOfSib(n.left,sum);
int rsum = sumOfSib(n.right,sum);
if(n.left!=null && n.right ==null){

sum =sum + n.left.data;

}
if(n.right!=null && n.left ==null){
sum = sum +n.right.data;

}
return sum+lsum+rsum;
}



The above code in Java

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

Initially the sum in zero and then you add the root data to the sum to get the final answer

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

Hey all... What do you guys think about the following code?

static int sum = 0;

int findSum(node *tree)
{
    if(tree->left == null && tree->right ==null)
    {
        // Means that it is a leaf node...

        sum += sum->value;
        return;
    }

    if(tree->right != null)
        findSum(tree->right);

    if(tree->left != null)
        findSum(tree->left);

}

- Rahul Arakeri January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

return type can be changed to void instead of int

- Rahul Arakeri January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int noSibSum(node* root)
{
    if(root == NULL ||(root->left==NULL && root->right == NULL))
    return 0;
    if(root->left!=NULL && root->right == NULL)
      return root->left->data + noSibSum(root->left);
      else if(root->right!=NULL && root->left == NULL)
      return root->right->data+noSibSum(root->right);
      else{
          return noSibSum(root->left)+noSibSum(root->right);
      }
}

- grave July 28, 2012 | 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