Microsoft Interview Question for Software Engineer / Developers






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

Should be simple...Convert the tree into a doubly linked list and then find the required two nodes by placing a pointer in the front and one at the end.....

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

this requires space O(n)

- momo July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No it doesnt, you are converting the given tree into doubly linked list... and hence you are not using any extra space... the latter part is pretty straight forward....

- chamy50 July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hmmm there's probably something i'm missing here. when you convert your tree to a linked list you need n nodes in your linked list to represent the n nodes in the tree. can you please explain the solution in more details, i'm really interested in knowing how a tree can be converted to a linked list without using the extra space for storing the list itself. thanks!

- momo July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nevermind, i found out how. thanks

- momo July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Boss, how do u maintain the tree structure ?

- NoMind December 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

BST data structure supports the operations of Min, Max, Predecessor and Successor. Just like in a sorted array we increment the pointer from min value and decrement the pointer from max value to check the sum, we repeatedly use the pointers returned by Successor and Predecessor functions in a BST to check the sum.

- Murali Mohan June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Finding predecessor and successor is O(logN). This will increase the overall complexity to O(NlogN).

- Guess Who?? June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you've parent pointer, it's amortized O(1) complexity to find predecessor/successor. You also don't need stack to compute - hence O(1) space too.

- lol June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, calculating inorder successor and predecessor would take log n time, but, the total amortized complexity would still be linear.

Here is an intuitive reasoning: Assume you are at node x, which has both left and right children. You would have passed through x once before to its left sub-tree, and would have reached x (which is the inorder successor of the predecessor of x in its left sub-tree). So far we have visited x twice. Now, to find the inorder successor of x, you would move to its right sub-tree, and while calculating the inorder successors of all the elements in the right sub-tree, except for the largest one, you would never visit x. Finally, you would have to pass through x one final time to find the inorder successor of the largest element in the right-subtree of x. So, you would visit each node a maximum of 3 times when performing inorder traversal only by calling its inorder successor.

The same applies for predecessor. Hence, I think we could perform the same task as we would on an array, by just calling inorder successor and predecessor instead of incrementing and decrementing pointers respectively.

Correct me if I'm wrong or have overlooked something.

- Viswanath S June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. I thought similar approach. It's iterative solution (implies no extra space, even on runtime stack), and it's O(n) complexity to find all pairs of (X,Y) whose key sum = K.

- lol June 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You have parent pointer at the tree structure to facilitate iterative traversal. You can also modify the tree temporarily if use Morris traversal.

- anonymous June 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok if you do morris traversal then you reduce tree to sorted doubly linked list and the solution is equivalent to sorted array solution.

- Ashish Kaila June 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we can use inorder traversal with recursion and the store it in an array (that will be sorted ). Will it work ??

- Aditya June 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

space O(1)

- pansophism June 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void sumSearch(Node nd1, Node nd2, sum){
    if(nd2 == null){
        search sum - nd1 in tree nd1;
        if (found)
          print;
        else{
            searchSum(nd1.left, nd1.right, sum);
        }
    }
    if(nd1 == null){
        search sum - nd2 in tree nd2;
        if (found)
          print;
        else{
            searchSum(nd2.left, nd2.right, sum);
        }
    }
	
	if(nd1 + nd2 > sum){
	    sumsearch(nd1.left, nd2);
	    sumsearch(nd1, nd2.left);
	} else if(nd1 + nd2 == sum){
	    print
	} else {
	    sumSearch(nd1.right, nd2);
	    sumSearch(nd1, nd2.right);
	}
    
}

- pansophism June 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

pansophism,
Can you please elaborate or write your algorithm?

- sanj July 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That works, but it's O(n) space complexity.

- magritte128 July 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You forgot at the last else sumSearch(nd1.right, nd2.right);

- Marius March 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am doubting if timeO(n) is possile

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

i cant find a sol for this even if it is not a tree but a sortd arry

- krishna June 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this question is equivalent to the finding of two values in a sorted array that sum to some value.

- AL June 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes! Possible if we have parent pointers!

- Anonymous June 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think so. Even if you have parent pointer, I am doubting you can do that in linear time O(n).

- Anonymous June 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Travesre the Tree in Inorder and store the elements and the corresponding pointers to nodes in a tree in an array in O(n) time........Then the code as follows

void fun(int arr[,int sum,int *a,int*b)
for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
{
      if(a[i]+a[j]==sum) 
       {
          *a=i;
           *b=j;
           exit(0);
          }
  }

- sameer.muthu9 June 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but this will consume O(n) space .

- krishna June 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

also time is O(n2)

- Old monk June 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@krishna : Can u please post the correct solution in O(n) time and O(1) space pls....

- sameer.muthu9 June 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem can be reduced to finding the sum on a sorted array (I'll get back to the tree in a sec). For this part, we use 2 pointers one at the start and one at the end, evaluate the sum and adjust accordingly (increment the lower pointer to add or decrement the higher pointer to substract) until the result is found or the pointers meet.

How do we achieve the same traversal on a tree? Use inorder for the lower pointer and a "reverse" inorder (right -> root -> left) for the higher one. If recursion is not allowed then use the Morris algorithm.

- Chong Ovalle June 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried the Chong Ovalle's solution in Java, I have something that's working, I just check if the sum is equal to 19 and stop the recursion, but I don't know about the complexity. Could anyone give me some feedback?

boolean found = false;
public void inOrderDouble(Node root_left, Node root_right, boolean leftRec){
if(root_left==null && leftRec) return;
if(leftRec)
inOrderDouble(root_left.left,root_right,true);


if(root_right==null && !leftRec) return;

inOrderDouble(root_left,root_right.right, false);



if(root_left.value + root_right.value == 19){
System.out.println(root_left.value + " "+ root_right.value);
found = true;

}
if(!found)
inOrderDouble(root_left,root_right.left, false);
if(!found)
inOrderDouble(root_left.right,root_right, true);
}

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

Hi Guys , I converted BDT in to Sorted DLL (it will take o(n) time & O(1) space) then find the two nodes in DLL which equals to given number
shashank7s dot blogspot dot com/2011/06/wap-to-find-two-nodes-into-binary dot html
let me know if anything wrong ,you can comment

Shashank

- WgpShashank June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, this should work.

- Somebody June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shashank, The DLL can again be converted back into BST in O(N). But, the problem is that the structure of the BST will change as DLL to BST conversion will result into balanced BST.

- Aashish June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sfmkflsdkf

- ashok June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

fuck u ash

- anonymous June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

People are looking for a reliable/valuable answers. Please don’t give shit answers. If you really don’t know then keep silent and read.
Kind request.

- Siva June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

great Logic by sashank

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

Step 1: Do Inorder traversal in O(n) to convert to array which will be sorted since it is BST.

Step 2 :: Use the sorted array:: Code in C#

class Program
{
static void Main(string[] args)
{
int[] a = new int[] {1,2,3,5,6,8,10,13,14,15,18,20,29,30};
OutputNode o = Search2BSTNode(a, 0, a.Length - 1,29);
if (o == null)
{
Debug.WriteLine("Value not found");
return;
}
Debug.WriteLine(String.Format(" start:{0} value:: {1} end: {2} value:: {3}",o.StartIndex,o.StartValue,o.EndIndex,o.EndValue));
}

private static OutputNode Search2BSTNode(int[] a, int start, int end, int k)
{
int mid = (start + end)/2;
int x = a[start], y = a[mid];
if( (x + y) == k)
{
if (start == mid) return null; //Because only one value is found.
return new OutputNode(start,x,mid,y); //Good
}
if (start < end)
{
if (y > k) // sum is less than middle element so on left half.
{
end = mid;
return Search2BSTNode(a, start, end, k);
}
if ((x + y) > k) // shift middle towards left.
{
end--;
return Search2BSTNode(a, start, end, k);
}
// (x+y) < k
{
start++; //shift middle towards right.
return Search2BSTNode(a, start, end, k);
}
}
return null;
}

public class OutputNode
{
private int _startIndex;
private int _startValue;
private int _endIndex;
private int _endValue;

public OutputNode(int startIndex, int startValue, int endIndex, int endValue)
{
_startIndex = startIndex;
_startValue = startValue;
_endIndex = endIndex;
_endValue = endValue;
}
#region Properties
public int StartIndex {get { return _startIndex; }}
public int StartValue { get { return _startValue; }}
public int EndIndex { get { return _endIndex; }}
public int EndValue { get { return _endValue; }}
#endregion

}

}

Let me know if its an issue. I tested it and it working but maybe i missed something.

- Kash007 July 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- the sum should be always > the node, if not move left

void findNodes(int k , Node *r)
{

if (r == null) return;

while(r->data > sum) //
r = r->r->left;

int d = k - r ->data;

if(findSecodNode(d))

cout<< r-> data <<" "<< d;

else
{

findNodes(k,r->left);
findNodes(k,r->right);
}

}
}

- teaser July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//the sum should be always > the node, if not move left

void findNodes(int k , Node *r)
{

if (r == null) return;

while(r->data > k) // go to the node which is less than k
r = r->left;

int d = k - r ->data;

if(findSecodNode(d))

cout<< r-> data <<" "<< d;

else
{

findNodes(k,r->left);
findNodes(k,r->right);
}

}

- teaser July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a combination of iterative inorder traversal, reverse in-order traversal and the algorithm to find two numbers in a sorted array whose sum is k

- DarkKnight July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am also treating the tree as a sorted array, but I find it difficult (if not impossible) to simulate pointing to the first & last node, then advancing either pointer recursively. Note that I am assuming we don't have a parent pointer for each node, otherwise it would be easier. So instead of starting off with first & last node, I am going to start off with the middle 2 nodes instead. The root is one of them, and I will try using both its left & right children as the other "middle" node. Hence the 2 calls in findNodes().

Idea is to recursively compare the current sum with K. If current sum is equal, we found the 2 nodes. If current sum < K, we can increase the sum by trying with the right child of either node. Similarly if current sum > K.

I haven't proved that starting from the middle will work, but I did test my code against several cases and got the right answers so far. This solution is O(n) time and O(1) space. It can be further optimized by avoiding having to call findNodes() twice, but that would still be O(n).

static Node[] findNodes(Node n, int K) {
	Node[] result = findHelper(n.left, n, K);
	if(result != null)
		return result;
	else return findHelper(n, n.right, K);
}

static Node[] findHelper(Node left, Node right, int K) {
	if(left == null || right == null || left == right)
		return null;
	if(left.value + right.value == K) {
		Node[] result = new Node[2];
		result[0] = left;
		result[1] = right;
		return result;
	} else if(left.value + right.value < K) {
		Node[] result = findHelper(left.right, right, K);
		if(result != null)
			return result;
		else return findHelper(left, right.right, K);
	} else {
		Node[] result = findHelper(left.left, right, K);
		if(result != null)
			return result;
		else return findHelper(left, right.left, K);
	}
}

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

firstly compute the k=sum-root element now find this k element in tree...

- anonymous programmer July 05, 2013 | 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