Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Do a post order traversal of the linked list and when the node is processed sumup the left , right and root node.

- KC January 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

bool isLeaf(Node *n)
{
if (n == NULL);
return false;

if (n->left == NULL && n->right == NULL)
return true;
else
return false;
}
update2SumTree(Node *n)
{
if (n == NULL)
return;
update2SumTree(n->left);
update2SumTree(n->right);

int sum_left = 0;
if (n->left)
{
if(isLeaf(n->left))
sum_left = n->left->data;
else
sum_left = 2 * (n->left-data);

}

int sum_right = 0;
if (n->right)
{
if(isLeaf(n->right))
sum_right = n->right->data;
else
sum_right = 2 *(n->right->data);
}

n->data = sum_right + sum_left;
}

- xhengdf@gmail.com December 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming only leaf nodes have values:

int UpdateTree(Node* n) {
    if(n==NULL) return 0; // check if the pointer is NULL, this is needed
                          // if the root is NULL, or if a node has only 1
                          // child and we call this function on both
                          // children of that node. see below.
    if(n->left || n->right) // if at least one child exists,
                            // update the value of the current node.
        n->data = UpdateTree(n->left)+UpdateTree(n->right);
    return n->data;
}

If internal nodes also have values that need to be summed with the subtrees, we don't need to check if at least one child exists. We can just add the values.

int UpdateTree(Node* n) {
    if(n==NULL) return 0;
    n->data += UpdateTree(n->left)+UpdateTree(n->right);
    return n->data;
}

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

It's either I understand the question wrong or you code is incorrect

- Yefei Wang December 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You'll have to elaborate on that. What do you think the question is? Why do you think my code is wrong?

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

can we have an iterative solution for this?

- Vineet Setia December 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but I guess that with extra data structure (stack?) or if thre are pointers up from children to their father.

- yes.... February 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the space complexity of this code?

- Vineet Setia December 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the space complexity of DFS?

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

3                              24        
                  /    \                          /     \
                2      4                     3        18
             /          /   \                 /          /   \
          1          6     8      =>  1          6   8

Is this correct example ?

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

//Gets the sum of children given the root node.
int Sum(Node* root)
{
if(root== NULL ) return 0;
return Sum(root->llink) + root->data + Sum(root->rlink);
}
//Gets the sum of children of every node.
int SumNode(Node* root)
{
if(root == NULL) return 0;

root->data = Sum(root->llink) + Sum(root->rlink);

SumNode(root->llink);
SumNode(root->rlink);
}

- sujith January 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sujith : your solution is O(n2) complexity. can we get this in O(n) ?

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

public static int sum(BSTree t) {
if(t.leftChild!=null && t.rightChild!=null)
t.value = sum(t.leftChild)+sum(t.rightChild);
if(t.leftChild!=null && t.rightChild==null)
t.value = sum(t.leftChild);
if(t.rightChild!=null && t.leftChild==null)
t.value = sum(t.rightChild);

return t.value;
}

I think this might work ....

- abhishek January 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will help..
int sum(struct node * head)
{int a,b,temp;
if(head==null)
return 0;
if(head->left==null && head->right==null)
{temp=head->data
head->data=0;
return temp;
}
temp=head->data;
a=sum(head->left);
b=sum(head->right);
head->data=a+b;
return(a+b+temp);
}

- anonymous January 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int SumNode(NODE *root)
{
if(root == NULL) return 0;
else
return (root->data = sumnode(root->llink) + root->data + sumnode(root->rlink));
}

- chidu January 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int update(node *x)
{
    if (!x)
        return 0;
    return x->inf += (update(x->left) + update(x->right));
}

- save.the.w. February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int update(node *x)
{
int data;
if (!x)
return 0;
data = x->inf;
x->inf += (update(x->left) + update(x->right));
return (data);
}

- Rashid April 26, 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