Microsoft Interview Question for Software Engineer in Tests


Team: SCVMM (Server tools)
Country: United States




Comment hidden because of low score. Click to expand.
1
of 3 vote

int returnClosest(node *root, int elem)
{
node *curr=root;
int smaller=-1,bigger=99999;

while(curr){
if(curr->data < elem){
smaller=curr;
curr=curr->right;
}
else if(curr->data > elem){
bigger=curr;
curr=curr->left;
}
else //equals
return curr->data;

} //end of while

return min(elem-smaller,bigger-elem);

}

- jobHunter September 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code will not work if the tree has negative data

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

I think the last line should be changed as

if(elem-smaller<bigger-elem) 
return smaller; 
else 
return bigger;

- pachunuri January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will not work for the following tree

10

1 11

and you need to find the closet of 9

- rrbd2 September 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code will not work if the tree has negative numbers

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

find inorder
o/p ill be order then easily find closest element..

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

find inorder
o/p ill be sort order then easily find closest element..

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

find inorder of given tree
o/p ill be sort order then easily find closest element..

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

the above code will work for negatives also, the only issues in that is compare abs( currentnumber - minNum ) & abs (currentnumber-MaxNumber) and take the one which gives the minimum.

Analysis is that, nearest number for any node would be left + rightmost and bigger number is Parent Or right-left most { remember this is what you use for deletion }


Traverse to the node, with a parent pointer all along.
then when node is found. break out of the loop

one broken out of the loop you should compare (parent-currentnode, right + left mostt-currentnode, left + right most t-currentnode)... which ever is the minimum you take the corresponding element
where

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

Easy (not optimal) solution is to store inorder traversal in array and then find closest

- pankaj January 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static BinaryTreeNode<Integer> closest(BinaryTreeNode<Integer> node, BinaryTreeNode<Integer> prevClosest, int n){
		if(node == null)
			return prevClosest;
		
		BinaryTreeNode<Integer> newClosest = prevClosest;
		if( Math.abs(node.data - n) < Math.abs(prevClosest.data - n) ){
			newClosest = node;
		}
		
		if(node.data < n ){
			return closest(node.right, newClosest, n);
		}
		else{
			return closest(node.left, newClosest, n);
		}
	}

- pankaj January 20, 2017 | 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