Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

int sumTree(Node root){
if(root==null)
return 0;
else
return root.data+sumTree(root.left)+sumTree(root.right);
}

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

what do you mean by sum of a binary tree, can you elaborate it a bit more?

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

Assuming the tree has nodes whose value is real numbers and we need to find the sum of the values of all the nodes, just perform a traversal (bfs/dfs) and keep adding them up.

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

//intial value of sum = 0 ; before calling must ensure that atleast root is there
void sumPath(struct node *pTree, int sum)
{
  if (!pTree) { cout<< sum<<endl;/*path not asked for*/
  sum+=pTree->data;
  sumPath(pTree->left, sum);
  sumPath(pTree->right,sum);

}

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

int tree_sum(node* ptr)
{

if(ptr!=NULL)
return ptr->key + tree_sum(ptr->left) + tree_sum(ptr->right);

return 0;

}

int main()
{

int sum=tree_sum(root);

return 0;

}

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

package com.careercup;

/**
 * Sum of each element in a binary tree
 * 
 * @author root
 * 
 */
public class BinaryTreeSum {
    public static void main(String args[]) {
        BinaryTree tree = new BinaryTree();
        Node root = tree.createTree(3, 0);
        // tree.displayTree_PreOrder(root);
        System.out.println("PreOder Sum:" + tree.sumPreOder(root, 0));
        System.out.println("InOder Sum:" + tree.sumInOder(root, 0));
        System.out.println("PostOderSum:" + tree.sumPostOder(root, 0));
    }
}

class Node {
    Node left;
    Node right;
    int element;
}

class BinaryTree {
    private static final int Min = 1;
    private static final int Max = 10;

    public Node createTree(int maxDepth, int curDepth) {
        if (curDepth == maxDepth) {
            return null;
        }
        Node node = new Node();
        node.element = Min + (int) (Math.random() * ((Max - Min) + 1));
        node.left = createTree(maxDepth, curDepth + 1);
        node.right = createTree(maxDepth, curDepth + 1);
        return node;
    }

    public void displayTree_PreOrder(Node node) {
        if (node == null) {
            System.out.println();
            return;
        }
        System.out.printf("%3d", node.element);
        displayTree_PreOrder(node.left);
        displayTree_PreOrder(node.right);
    }

    public double sumPreOder(Node node, double sum) {
        if (node == null) {
            return 0;
        }
        sum = node.element + sumPreOder(node.left, sum) + sumPreOder(node.right, sum);
        return sum;
    }

    public double sumInOder(Node node, double sum) {
        if (node == null) {
            return 0;
        }
        sum = sumPreOder(node.left, sum) + node.element + sumPreOder(node.right, sum);
        return sum;
    }

    public double sumPostOder(Node node, double sum) {
        if (node == null) {
            return 0;
        }
        sum = sumPreOder(node.left, sum) + sumPreOder(node.right, sum) + node.element;
        return sum;
    }
}

PreOder Sum:33.0
InOder Sum:33.0
PostOderSum:33.0

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

You don't really need the "double sum" parameter in your traversal methods

- Anonymous January 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.careercup;

/**
 * Sum of each element in a binary tree
 * 
 * @author root
 * 
 */
public class BinaryTreeSum {
    public static void main(String args[]) {
        BinaryTree tree = new BinaryTree();
        Node root = tree.createTree(3, 0);
        // tree.displayTree_PreOrder(root);
        System.out.println("PreOder Sum:" + tree.sumPreOder(root, 0));
        System.out.println("InOder Sum:" + tree.sumInOder(root, 0));
        System.out.println("PostOderSum:" + tree.sumPostOder(root, 0));
    }
}

class Node {
    Node left;
    Node right;
    int element;
}

class BinaryTree {
    private static final int Min = 1;
    private static final int Max = 10;

    public Node createTree(int maxDepth, int curDepth) {
        if (curDepth == maxDepth) {
            return null;
        }
        Node node = new Node();
        node.element = Min + (int) (Math.random() * ((Max - Min) + 1));
        node.left = createTree(maxDepth, curDepth + 1);
        node.right = createTree(maxDepth, curDepth + 1);
        return node;
    }

    public void displayTree_PreOrder(Node node) {
        if (node == null) {
            System.out.println();
            return;
        }
        System.out.printf("%3d", node.element);
        displayTree_PreOrder(node.left);
        displayTree_PreOrder(node.right);
    }

    public double sumPreOder(Node node, double sum) {
        if (node == null) {
            return 0;
        }
        sum = node.element + sumPreOder(node.left, sum) + sumPreOder(node.right, sum);
        return sum;
    }

    public double sumInOder(Node node, double sum) {
        if (node == null) {
            return 0;
        }
        sum = sumInOder(node.left, sum) + node.element + sumInOder(node.right, sum);
        return sum;
    }

    public double sumPostOder(Node node, double sum) {
        if (node == null) {
            return 0;
        }
        sum = sumPostOder(node.left, sum) + sumPostOder(node.right, sum) + node.element;
        return sum;
    }
}

Output
----------
PreOder Sum:42.0
InOder Sum:42.0
PostOderSum:42.0

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

class Program
{
static void Main(string[] args)
{
BinaryTree tree = new BinaryTree();
tree.Add(10);
tree.Add(4);
tree.Add(9);
tree.Add(122);
tree.Add(56);
tree.Add(1);
tree.Add(2);
tree.TraverseTree();
Console.WriteLine("Sum of tree: " + tree.GetSumOfTree());

Console.ReadLine();
}
}

class BinaryTree
{
Node header;

public BinaryTree()
{
header = null;
}

public void TraverseTree()
{
if (header == null) Console.WriteLine("The tree is empty!");

DoTraverseTree(header);
Console.WriteLine();
}

private void DoTraverseTree(Node node)
{
if (node == null) return;

Console.Write(node.Data + " ");
DoTraverseTree(node.leftChild);
DoTraverseTree(node.rightChild);
}

public void Add(int data)
{
Node newNode = new Node(data);

if (header == null)
{
header = newNode;
return;
}

Node current = header;
while (true)
{
if (current.Data < data)
{
if (current.rightChild == null)
{
current.rightChild = newNode;
return;
}
else
{
current = current.rightChild;
}
}
else
{
if (current.leftChild == null)
{
current.leftChild = newNode;
return;
}
else
{
current = current.leftChild;
}
}
}
}

public int GetSumOfTree()
{
if (header == null) return 0;

return DoGetSumOfTree(header);
}

private int DoGetSumOfTree(Node node)
{
if (node == null) return 0;

int sum = node.Data;
if (node.leftChild != null) sum += DoGetSumOfTree(node.leftChild);
if (node.rightChild != null) sum += DoGetSumOfTree(node.rightChild);

return sum;
}
}

class Node
{
public int Data { get; set; }
public Node leftChild { get; set; }
public Node rightChild { get; set; }

public Node(int data)
{
this.Data = data;
}
}

- Prince Wang May 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
struct node* left,*right;
}*root=NULL;
void addo(struct node **root,int a[],int index,int n)
{
if(a[index]!=-1)
{
*root=new node;
(*root)->data=a[index];
addo(&(*root)->left,a,2*index+1,n);
addo(&(*root)->right,a,2*index+2,n);
}
else
{
*root=NULL;
}
}
void inorder(struct node* root)
{
if(root)
{
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
void sum_binary_tree(struct node *p,int* sum)
{
if(p)
{
if(p->left!=NULL && p->right!=NULL)
{
*sum=*sum+p->left->data+p->right->data;
sum_binary_tree(p->left,sum);
sum_binary_tree(p->right,sum);
}
else if(p->left==NULL && p->right!=NULL )
{
*sum+=p->right->data;
sum_binary_tree(p->right,sum);
}
else if(p->left!=NULL && p->right==NULL)
{
*sum+=p->left->data;
sum_binary_tree(p->left,sum);
}
}
}

int main()
{
int n;
cout<<"enter the num of elements:\n";
cin>>n;
int a[2*n+1],i;
cout<<"enter the elements\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=n;i<2*n+1;i++)
a[i]=-1;
addo(&root,a,0,n);
inorder(root);
int *sum;
sum= new int;
*sum=root->data;
sum_binary_tree(root,sum);
cout<<"sum is "<<*sum;
getch();
}

- Nikhita May 14, 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