Microsoft Interview Question for Software Engineer / Developers


Team: yammer
Country: United States




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

/* height of Binary tree recursively */
	private static Integer heightRecursively(BinaryTreeNode root) {
		int leftHeight, rightHeight;
		if (root == null)
			return 0;
		else {
			leftHeight = heightRecursively(root.getLeft());
			rightHeight = heightRecursively(root.getRight());
			if (leftHeight > rightHeight)
				return leftHeight + 1;
			else
				return rightHeight + 1;
		}
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
9
of 9 vote

/* height of Binary tree iteratively */
	private static Integer height(BinaryTreeNode root) {
		BinaryTreeNode temp;
		Queue<BinaryTreeNode> q = new LinkedBlockingDeque<BinaryTreeNode>();
		q.add(root);
		q.add(new BinaryTreeNode(-1));
		int level = 0;
		while (!q.isEmpty()) {
			temp = q.poll();
			if (temp.getData() == -1) {
				if (!q.isEmpty())
					q.add(new BinaryTreeNode(-1));
				level++;
			} else {
				if (temp.getLeft() != null)
					q.add(temp.getLeft());
				if (temp.getRight() != null)
					q.add(temp.getRight());
			}
		}

		return level;
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
7
of 7 vote

height is usually defined as max depth of any node

h( Node x )
{
     if( x == nil ) return 0; //tree rooted at nil has 0 height
   
	//tree rooted here is  1+ height of tallest subtree
     return   max( h(x.left) , h(x.right) )  +1 ;  
}

- S O U N D W A V E October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess it would be more appropriate to call these funcitons num_levels.

- S O U N D W A V E October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int getHeight(Node root){

if(root==null) return 0;

int L=0;
int R=0;

if(root.L!=null) L=1+getHeight(root.L);

if(root.R!=null) R=1+getHeight(root.R);

return max(L,R);
}

- MrA October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

Do not need to check if left child or right child is null. Already included in the base case

- maillist.danielyin October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Max_height_tree {
	
	int height1,height2;
	
	public Max_height_tree() {
	height1 = 0;
	height2 =0;
	}
	
	public int height(TreeNode node)
	
	{	
		if(node==null)
			return -1;//This decides if a 
		else{
		
		height1=height(node.leftchild)+1;
		height2 =height(node.rightchild)+1;
		
		}
		return (Math.max(height1,height2));
		
		
	}

}

- RS October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here I`ll post a non-recursive way.
int height(TreeNode * root){
if (!root) return 0;
stack<TreeNode *> s;
TreeNode * current = root;
int maxHeight = 0;
while(true){
if (current){
s.push(current);
current = (current->left != NULL)?current->left:current->right;
}
else{
maxHeight = s.size()>maxHeight?s.size():maxHeight;
current = s.top();
s.pop();
if (s.empty()) break;
if (current == s.top()->left){
current = s.top()->right;
}
else{
current = NULL;
}
}

}
return maxHeight;
}

- maillist.danielyin October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void MaxHeight(struct bst *b, int level, int &maxHeight, struct bst **MaxHeightNode)
{
	if(b)
	{
		MaxHeight(b->lc,level+1, maxHeight, MaxHeightNode);
		if(level>maxHeight)
		{
			maxHeight=level;
			(*MaxHeightNode)=b;
		}
		MaxHeight(b->rc,level+1, maxHeight, MaxHeightNode);
	}
}

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

//Java code

int getHeight (TreeNode root) {
if (root == null)
return 0;
else
return 1 + Math.max ( getHeight (root.left), getHeight (root.right) );
}

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

public static int maxDepth;

public static void Width(Node tree)
        {
            var queue = new Queue<KeyValuePair<int, Node>>();
            queue.Enqueue(new KeyValuePair<int, Node>(0, tree));

            while (queue.Count > 0)
            {
                var node = queue.Dequeue();
                var level = node.Key;
                maxDepth = Math.Max(maxDepth, level);
                foreach(var child in node.Value.Children)
                    queue.Enqueue(new KeyValuePair<int, Node>(level + 1, child));
            }
        }

- Anonymous March 02, 2014 | 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