Microsoft Interview Question for Software Engineer in Tests






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

bool ContainsSum(btree *root, int N)
{
if(!root)
return(N == 0)
else{
int remsum = N - root->value;
return((ContainsSum(node->left, remsum)) || (ContainsSum(node->right, remsum)));
}
}

- Anonymous February 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Should it check N==0 at leaf node

- Dpac May 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a small change in the above code pls replace node by root

- Anonymous February 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How does this code return the actual path from the root node to the leaf ? I think it just returns whether such a path exists or not.

Even that it doesnt as there is a possibility of sum getting equal to the given value at an interior node whereas the question specifically says for leaf

- Anonymous February 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 votes

It is simple, just change from the above code, but in java:
{ private Vector<Integer> findPath(BSTNode subroot, int sum, Vector<Integer> path){
if(subroot == null){
if(sum == 0)
return path;
else{
return null;
}
}
else{
sum -= subroot.getValue().intValue();
Vector<Integer> newPath = new Vector<Integer>(path);
newPath.add(subroot.getValue());
Vector<Integer> leftResult, rightResult;
leftResult = findPath(subroot.getLeft(), sum, newPath);
rightResult = findPath(subroot.getRight(), sum, newPath);

if(leftResult==null && rightResult == null)
return null;
else{
if(leftResult == null)
return rightResult;
else
return leftResult;
}
}
}}

- Anonymous February 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/// <summary>
        /// Prints the paths to next smallest node.
        /// </summary>
        /// <param name="root">The root.</param>
        /// <param name="nodeValueToSearch">The node value to search.</param>
        public void printSumOfPathsToANode(BinaryTreeNode root, int nodeValueToSearch)
        {
            var path = new int[1000];
            printSumOfPathsToANode(root, path, 0, nodeValueToSearch);
        }

        /// <summary>
        /// Recursive printPaths helper -- given a node, and an array containing
        /// the path from the root node up to but not including this node,
        /// prints out all the root-leaf paths.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="path">The path.</param>
        /// <param name="pathLen">The path len.</param>
        /// <param name="nodeValueToSearch">The node value to search.</param>
        private static void printSumOfPathsToANode(BinaryTreeNode node, int[] path, int pathLen, int nodeValueToSearch)
        {
            if (node == null) return;

            nodeValueToSearch -= node.Name;
            // append this node to the path array
            path[pathLen] = node.Name;
            pathLen++;


            // it's a leaf, so print the path that led to here
            if (nodeValueToSearch == 0)
            {
                printArraySumOfPathsToANode(path, pathLen);
            }
            else
            {
                // otherwise try both subtrees
                printSumOfPathsToANode(node.Left, path, pathLen, nodeValueToSearch);
                printSumOfPathsToANode(node.Right, path, pathLen, nodeValueToSearch);
            }
        }

        /**
         Utility that prints ints from an array on one line.
        */
        private static void printArraySumOfPathsToANode(int[] ints, int len)
        {
            Console.WriteLine("New Path Started");

            int i;
            for (i = 0; i < len; i++)
            {
                Console.WriteLine(ints[i] + " ");
            }
            Console.WriteLine("Path Ended");
        }

- Ganesh Narayanan March 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think DFS can be used.

- Anonymous July 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree.

- cirus October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use the logic as given for the first response and tweak it a bit for adding the path for each traversal of nodes and you're done!

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

this is the working code for a binary tree, can be easily expanded to use for n-ary tree:
<code snippet in CPP>

typedef struct node {
    int data;
    struct node *left;
    struct node *right;
}Node;

void makeBTree(string givenString)throw() {
    cout << "given input= " << givenString << endl;
    stack<Node *> nodeStack;
    Node *temp;
    int ix=0;

    while(givenString[ix] !='\0') {
        if (isdigit(givenString[ix])) {
            temp = new Node;
            temp->data = givenString[ix] - '0';
            temp->left = NULL;
            temp->right = NULL;
            nodeStack.push(temp);
        }
        if (givenString[ix]==')' && nodeStack.size()>1) {
            temp = nodeStack.pop();
            Node *last = nodeStack.pop();
            if (last->left == NULL) {
                last->left = temp;
            } else if (last->right == NULL) {
                last->right = temp;
            }
            nodeStack.push(last);
        }

        ix++;
    }
}

test run:
i/p: (1(2(3)(4))(5(6)))
o/p: 3 2 4 1 6 5 //inorder traversal, to test the validity

- googler September 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// returns true if the tree has a path with the specified sum
static int
hasPathSum(node_t *subtree, int sum)
{
	if (subtree && (((subtree->left == subtree->right) && subtree->key == sum) || 
		(hasPathSum(subtree->left, sum - subtree->key) || hasPathSum(subtree->right, sum - subtree->key)))) {
		cout << subtree->key << ", ";
		return 1;
	}

	return 0;
}

- N November 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

call --> findPathOfLen_N(root, 0, N)

findPathOfLen_N(node* curr, K, N)
{

if(currr == NULL){
cout << "No such path exists";
return false;
}

if(curr->data + K == N)
if(curr->left == NULL && curr->right == NULL)
{
print(curr->data);
return true;
}

if(curr->data->left != NULL)
if (function(curr->left, K-curr->data, N))
return true;
if(curr->data->right != NULL)
if (function(curr->right, K-curr->data, N))
return true;

return false;
}

- .... December 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool ContainsSum(btree *node, int N)
{
if(node==NULL)
return(N == 0)
return((ContainsSum(node->left, N-node->data)) || (ContainsSum(node->right, N-node->data)));
}

- Rocky February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool ContainsSum(btree *node, int N)
{
if(node==NULL) return(N == 0);
return((ContainsSum(node->left, N-node->data)) || (ContainsSum(node->right, N-node->data)));
}

- Rocky February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how about below binary tree,get path 10.
###in fact,2 is not leaf node

8
2
5

- anonymous August 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

olution can be found on stanfords website
hxxp://cslibrary.stanford.edu/110/BinaryTrees.pdf (similar to Rockey solution)

- Stag March 01, 2010 | 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