Microsoft Interview Question for Software Engineer / Developers


Country: United States




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

Use BFS to find the closest leaf node first and then when the bfs exits, you have the leaf at the farthest level. Find the difference.

- Primus October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

BFS or DFS in this situation work pretty much in the same way for timing.

- marcelovox2 October 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

int distance_to_leaf(Node* root) {
  vector<pair<Node*, int> > stack;
  stack.push_back(make_pair(root, -1));
  int maxDist = -1;
  while (stack.size() > 0) {
    pair<Node*, int> curr = stack.back();
    Node* n = curr.first;
    stack.erase(stack.end()-1);
    if (!n->left && !n->right) 
      maxDist = max(maxDist, curr.second);
    if (n->left) 
      stack.push_back(make_pair(curr.first->left, curr.second+1));
    if (n->right)
      stack.push_back(make_pair(curr.first->right, curr.second+1));
  }
  return maxDist;
}

- Martin October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

//Modifying ti get what is required by interviewer

int FindDiffBwMinMaxdisttoleaf(Node* root, int *diff) {
  vector<pair<Node*, int> > stack;
  stack.push_back(make_pair(root, -1));

  int maxDist = -1;
  int minDist = (1<<31)-1;
  int *diff = 0;

  while (stack.size() > 0) {
    pair<Node*, int> curr = stack.back();
    Node* n = curr.first;
    stack.erase(stack.end()-1);
    if (!n->left && !n->right){ 
      maxDist = max(maxDist, curr.second);
      minDist = min(minDist, curr.second);
    }
    if (n->left) 
      stack.push_back(make_pair(curr.first->left, curr.second+1));
    if (n->right)
      stack.push_back(make_pair(curr.first->right, curr.second+1));
  }
  *diff = maxDist-minDist;
  return diff;
}

- googlybhai October 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think stack can not be used

- Srikant Aggarwal October 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the stack (which is actually a vector) Martin uses is not the same as the "stack" mentioned by the interviewer, the interviewer meant the call stack caused by recursion......

- penny October 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

though I think using a queue instead of vector, same idea, but seems simpler for understanding....: what do you guys think ? :)

int difference(Node* root)
{
    if(!root)
        return 0;
    int closest = 0;
    int furthest = 0;
    queue<pair<Node*, int>> nodes;
    nodes.push(pair<Node*, int>(root,0));
    while(!nodes.empty()) {
        pair<Node*, int> n=nodes.front();
        nodes.pop();
        if(!n.first->left&&!n.first->right) {
            if(closest == 0) {
                closest = n.second;
            } else {
                furthest = n.second;
            }
        } else {
            if(n.first->left) {
                nodes.push(pair<Node*,int>(n.first->left,n.second+1));
            } 
            if(n.first->right) {
                nodes.push(pair<Node*,int>(n.first->right,n.second+1));
            }
        }
    }
    return (furthest>closest)?(furthest-closest):0;
}

- penny October 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Martin solution gives the height of the tree

- VR November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is my solution in C# using two lists, one is for the current level being examined and the other one is for the next level. Length of the lists are not dependent on the depth of the tree. Actually it traverses the tree level by level like BFS.

public static int GetDifference(TNode node)
{
    int minHeight = Int32.MaxValue;
    int level = 0;
    List<TNode> list = new List<TNode>();
    list.Add(node);
    List<TNode> next = new List<TNode>();
    while (list.Any())
    {
       level++;
       next.Clear();
       foreach (TNode n in list)
       {
          if (n.left != null)
             next.Add(n.left);
          if (n.right != null)
             next.Add(n.right);
          if (n.left == null && n.right == null)
          {
              if (minHeight > level)
                minHeight = level;
          }
    }
         list.Clear();
         list.AddRange(next);                   
  }
    return level - minHeight;
}

- berkaypamir November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Iterative post oder traversal will do the trick here!

- Sharat October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even preorder (or inorder) will do.

Preorder is much easier to code than post-order.

- Anonymous October 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Use DFS algorithm to get the height of each path from the root to the leaf.

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

If using BFS and the tree is not full/complete how do you keep track of depth?

- Tragic Foobar October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use a special node called marker node. First, push the root and the marker node into the queue. Then, Every time you encounter that node, increase the height and push the marker node back into the queue. (to avoid getting into a loop, you also have to check whether the queue is empty whenever you dequeue a marker node.)

- Aquarian October 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

DFS Iterative version:

private static int heightDiffFromClosestToFarthestLeafFromRoot(TreeNode01 root) {
if(root.left == null || root.right == null) {
System.out.println("There are no 2 leafs from the root");
return 0;
}
Stack<TreeNode01> s = new Stack<TreeNode01>();
TreeNode01 curr = root;
int maxHeight = -1, minHeight = Integer.MAX_VALUE;
int lh = 0, rh = 0;
boolean done = false; boolean lchild = false;
while (!done) {
if (curr != null) {
s.push(curr);
curr = curr.left;
if(curr != null) {
lchild = true;
lh++;
}
} else {
if (!s.empty()) {
curr = s.pop();

if(curr.left == null && curr.right == null) {
if(lh < minHeight && lh > 0) {
minHeight = lh;
if(rh < minHeight && rh > 0) {
minHeight = rh;
}
}
if(lh > maxHeight && lh > 0) {
maxHeight = lh;
if(rh > maxHeight && rh > 0) {
maxHeight = rh;
}
}

}
if(lchild) {
lh--;
lchild = false;
}

curr = curr.right;
if(curr != null) {
rh++;
}
}
else {
done = true;
}
}
}
return maxHeight - minHeight;
}

- BoredCoder October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above code has some problems. This is the corrected one:

private static int heightDiffFromClosestToFarthestLeafFromRoot(TreeNode01 root) {
Stack<TreeNode01> s = new Stack<TreeNode01>();
TreeNode01 curr = root;
int maxHeight = -1, minHeight = Integer.MAX_VALUE;
int height = 0;
boolean done = false;
boolean lchild = false;
while (!done) {
if (curr != null) {
s.push(curr);
curr = curr.left;
if (curr != null) {
lchild = true;
height++;
}
} else {
if (!s.empty()) {
curr = s.pop();
if (curr.left == null && curr.right == null) {
if (height > maxHeight) {
maxHeight = height;
}
if (height < minHeight) {
minHeight = height;
}
}
if (curr.right != null) {
lchild = false;
}
if (lchild) {
height--;
}
curr = curr.right;
if (curr != null) {
height++;
lchild = false;
}
} else {
done = true;
}
}
}
return maxHeight - minHeight;
}

- BoredCoder October 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class TreeNode {
   TreeNode left;
   TreeNode right;
}

class MyData {
    TreeNode node;
    int depth;
    public MyData(TreeNode n, int d) {
        node = n;
        depth = d;
    }
}

TreeNode root = …;

Queue<MyData> q = new Queue();
q.append(new MyData(root, 0));

int min = Integer.MAX_VALUE;
int max = 0;

while (MyData data = q.pop() != null) {
    if (data.left == null && data.right == null) {
        if (data.depth < min) {
            min = data.depth;
        }
        if (data.depth > max) {
            max = data.depth;
        } 

        continue;
    }
    if (data.left != null) {
        q.append(new MyData(data.left, data.depth + 1));
    }
    if (data.right != null) {
        q.append(new MyData(data.right, data.depth + 1));
    }
}

return max - min;

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

#include <iostream>
#include <cmath>

using namespace std;

//return number of digits
int num_digits(int n){
	int digit = 0;
	while(n>0){
		digit++;
		n = n/10;
	}
	return digit;
}

//return first-second
int mcompare(int first, int second){
	int diff;
	int nf = num_digits(first);
	int ns = num_digits(second);
	diff = (first*pow(10,ns)+second) - (second*pow(10,nf)+first);
	return diff;
}

void merge(int arr[], int begin, int mid, int end){
	int begin1=begin, end1=mid, begin2=mid+1, end2=end;
	int tarr[end-begin+1];
	int i=0;
	while(begin1<=end1 && begin2<=end2){
		if(mcompare(arr[begin1], arr[begin2])<0){
			tarr[i++] = arr[begin2++];
		}else{
			tarr[i++] = arr[begin1++];
		}
	}
	if(begin1>end1){
		while(begin2<=end2){
			tarr[i++] = arr[begin2++];
		}
	}else{
		while(begin1<=end1){
			tarr[i++] = arr[begin1++];
		}
	}
	i--;
	while(i>=0){
		arr[begin+i] = tarr[i];
		i--;
	}
}

void msort(int arr[], int begin, int end){
	if(begin == end){
		return;
	}
	if(begin+1 == end){
		if(mcompare(arr[begin], arr[end])<0){
			int tmp = arr[begin];
			arr[begin] = arr[end];
			arr[end] = tmp;
		}
		return;
	}
	int mid = (end-begin)/2 + begin;
	msort(arr, begin, mid);
	msort(arr, mid+1, end);
	merge(arr, begin, mid, end);
}

int main(int argc, char **argv){
	int arr[] = {9, 4, 94, 4, 14, 1};
	int n = sizeof(arr)/sizeof(int);
	//cout<<"\nn: "<<n;
	cout<<"\nInitial array: ";
	for(int i=0; i<n; i++){
		cout<<arr[i]<<" ,";
	}
	msort(arr, 0, n-1);
	cout<<"\nSorted array: ";
	for(int i=0; i<n; i++){
		cout<<arr[i]<<" ,";
	}
	return 1;
}

- surbhi October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int solve(TreeNode* root)
   {
           queue< pair<TreeNode*, int> > q;
           if (root != NULL) return q.push( make_pair(root, 1) );
          int ans(-1), closest(0) ; 
          while (!q.empty())
          {
                   TreeNode* cNode = q.top().first;
                   int cLevel = q.top().second;
                   if (is_leaf(cNode))  {
                           if (!closest) { closest = cLevel; } 
                          else { ans = cLevel - closest; } 
                   }
                   else {
                           if (cNode->left) q.push( make_pair(cNode->left, cLevel+1) );
                           if (cNode->right) q.push( make_pair(cNode->right, cLevel+1) );
                   }
          }
          return ans; 
   }

- Anonymous August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

assuming if bst has 1 leaf or no leaf, then ans is -1

- Anonymous August 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int DiffHeight(Tree *root, int min,int max, int height)
{
if(root == NULL)
return;
if(root->left == NULL && root->right == NULL)
{
if(height < min)
min = height;
if(height > max)
max = height;
}
else
{
DiffHeight(root->left, min, max, height+1);
DiffHeight(root->right, min, max, height+1);
}
}

- DashDash March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please look at the question again. No Recursion.

- Kevin March 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

The question mentioned a request to avoid using stack since the tree is too long. So this is an n^2 solution using O(1) memory:

public static int findLeavesDiff(ColorableNode tree) {
		int minLeaf = Integer.MAX_VALUE;
		int maxLeaf = Integer.MIN_VALUE;
		while(tree.hasDiscoloredLeft() || tree.hasDiscoloredRight()){
			ColorableNode node = tree;
			int level = 0;
			while(true){
				if(node.hasDiscoloredLeft()){
					level++;
					node = node.left;
				}else if(node.hasDiscoloredRight()){
					level++;
					node = node.right;
				}else{
					node.color = 1;
					if(node.isLeaf()){
						minLeaf = Math.min(level, minLeaf);
						maxLeaf = Math.max(level, maxLeaf);
					}
					break;
				}
			}
		}
		return (maxLeaf == Integer.MAX_VALUE) ? 0 : maxLeaf - minLeaf;
	}

- avico81 October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your method is wrong.
First, you cannot assume there is a color field in Binary Search TreeNode.
Second, your algo will always go left bottom until leaf...

- Kevin March 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

lol you could have equally said:

int answer = GiveMeTheAnswer(my_tree);

Now just implement GiveMeTheAnswer!

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

Give it!

- GiveMeTheAnswer October 23, 2012 | Flag


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