Microsoft Interview Question for Software Engineer / Developers






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

hmm not sure about this,
Do you mean sum of nodes traversed from root to leaf. Does the sum alywas include the root ?

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

findsum(root,sum){

if(sum<0) return 0;

if(sum==root->value) return -1;

if(root==NULL) return 0;

int num_node=0;

int right=findsum(root->right,sum-root->value);

if(right>0) num_node=right+1; return num_node;

else if(right<0) num_node=1;  return num_node;

int left=findsum(root->left ,sum-root->value);

if(left>0) num_node=left+1;  return num_node;

else if(left<0) num_node=1;  return num_node;

return 0;

- Anonymous January 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You transverse the tree in a Depth-first way, but that will not guarantee the smallest number of nodes:

sum = 8
4
/ \
4 1
\
1
\
2

- Fan January 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i dont understand what u r saying but idea here is check right subtree first then the left subtree..since it is bst,if u find answer in right subtree u r guaranteed to have least number of nodes..

other approach: Level order traversal.

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

@fan: do u know what bst means?

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

BST = Binary Search Tree

- russoue February 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@russoue: Do you know what sarcasm means?

- Anonymous November 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Anonymous .. the required sum and the data values in the nodes can also be negative.you need to take care of that..so going to the right subtree doesn't ensure that we reach the minimum first..

- Musheka January 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

bro its bst..if values in right subtree is neg..in the left would be more

- Anonymous January 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bro its bst..if values in right subtree is neg..in the left would be more

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

Call
minsum(root, sumToBeFound, 0, 0, MAX_NUMBER);

node *minsum(node *root, int sum,int parentsum, int depth, out int mindepth)
{
	if(null == root || mindepth == depth)
	{
		return null;
	}

	parentsum = parentsum + root->data;
	if(temp == sum)
	{
		minddepth = depth;2
		return root;
	}
	else if(temp > sum)
	{
		return null;
	}

	Node *temp1 = mindsum(root->left, sum, parentsum, depth + 1, minddepth);
 
	Node *temp2 = mindsum(root->right, sum, parentsum, depth + 1, mindepth);
	if(null != temp1)
	{
		return temp1;
	}
	else if(null != temp2)
	{
		return temp2;
	}

	return null;

}

- Anonymous January 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

changing the function arguement is not a good practice..in the Q its given root and sun only..so avoid adding more arg..i leave neg impression to the interviewers

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

I do agree that changing function arguments is not the good practice but the cases like this where one have to
a) Rely on size
b) Global variable (against the reentrant programming).

Its better to write a private function with desired no of arguments .SO this way we wrap around solution (this funda known as wrapper).

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

minsum(root,n)=min{1+minsum(root.left,n-root.data),1+minsum(root.right,n-root.data),minsum(root.left,n),minsum(root.right,n)}

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

fun (node,sum)
{
if(node==null && sum==0)
return true;
else if(node==null) //but sum!=0
return false;
else if(sum < node->key) //sum is less than node--> key so only search area is left side of the node
return fun(left,sum-node->key);
else
return (fun(left,sum - node->key)||fun(right,sum - node->key) //if sum is greater than node-->key then we have to search in both side and then take or of those two result
}

- gautam kumar February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a little correction in above

in third condtion
else if(sum < node -->key)
return false;

above code work only when numbers are +ve

- gautam kumar February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is correct.

- euv921 March 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Use BFS traversal (use queue)
2. at each node currentSum = sum obtained from parent + current value
3. if the currentSum is the required sum and node is leaf node, you have got solution
4. else if currentSum > required sum, do not add child
5. else
5.a Add left child to the queue;
5.b add right child to queue only if (requiredSum-currentSum<=currentSum)


BFS ensures minimum solution. 5.b and 4 steps prune search space

- rparundekar March 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* findNodeForSum( node *root, int& sum, const int desiredsum)
{
if( root == null )
{
return null;
}
else
{
sum += root.value;
if( sum == desiredsum &&
//ensure leaf
root->left == null &&
root->right == null )
{
return root;
}
else
{
Node *result = findNodeForSum( root->left, sum, desiredsum );
if( result == null )
{
Node *result = findNodeForSum( root->right, sum, desiredsum );
}
// if we are returning null decrement sum then we should return it to prior state
if( result == null )
sum -= root.value;
return result;
}
}
}

this was coded on the fly but should work.

- nd April 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but we need the shortest path ... !!
This thing you are not considering in your code /...

- broadcaster July 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public treeNode MinimumNodeFromRootHavingSum(treeNode curr, int sum)
{
if (curr == null || sum < 0)
return null;

int subsum = sum - curr.value;

if (subsum == 0)
return curr;

treeNode leftResult = MinimumNodeFromRootHavingSum(curr.leftChild, subsum);
treeNode rightResult = MinimumNodeFromRootHavingSum(curr.rightChild, subsum);

return (leftResult != null)? leftResult :
(rightResult != null)? rightResult : null;

}

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

geeksforgeeks.org/ ?p=6201

- Anonymous June 19, 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