Microsoft Interview Question for Software Engineer / Developers






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

double getBalanceFactor(node *root)
{
   double sum = 0;
   getBalFactCore(root,1,&sum);
   return sum;
}

int getBalFactCore(node *root, int level, double *sum)
{
   if (root==null)
      return 0;
   int desc = getBalFactCore(root->left,level+1,sum) + getBalFactCore(root->right,level+1,sum);
   *sum += desc*1.0/level;
   return desc+1;
}

- CyberS1ren November 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It would work!!
tried a few test cases and it seems to work..

- Mohit December 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution !!

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

Can somebody please explain the question? What does '(number of all its descendants/level of node);' mean :((

- random November 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think balance factor of a binary tree node is equal to the height of the left sub-tree minus the height of the right sub-tree.

The definition given in the question seems to be wrong...

- Anonymous November 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree...all i have ever read is that balance factor of a binary tree node is equal to the height of the left sub-tree minus the height of the right sub-tree..and not their sum...

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

I do not see anything wrong with the question. Just use recurrsion. Parameter sum is a pointer to an integer which is used to store the number of the current node's decendents plus the node itself.

int BF(Node root, int *sum, int depth) {
   if (root==null)
   {   *sum = 0;
       return 0;
   }
   int *sum1, *sum2, bf1, bf2, bf;
   bf1 = BF(root->left, sum1, depth+1);
   bf2 = BF(root->right, sum2, depth+1);
   *sum = *sum1 + *sum2 + 1;
   bf = *sum/depth;
   return bf + bf1 + bf2;
}

- nec December 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

guess this should work....
{

[sum,bf] findBF(node* root)
{
if(root==NULL)
return [0,-1];
[sum1,bf1]=findBF(root->left);
[sum2,bf2]=findBF(root->right);
bf = (bf1 + 1) + (bf2 + 1);
sum = sum1 + sum2 + bf;
return [sum,bf];
}

and final "sum" is what we need....

correct me if im wrong or if i had missed any case....
}

- rhino December 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int bfact(node *root)
{
if !(root)
return 0;
if(root->left ==NULL && root->right ==NULL)
return 0;
int bfact =0;
if(root->left !=NULL)
bfact = bfact(root->left) +count(root->left);

if(root->right !=NULL)
bfact = bfact(root->right) + count(root->right);

return bfact;
}

- aka December 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A correction
The second statement updating the int bfact shud be bfact +=
bfact(root->right) + count(root->right);

- aka December 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int bfact(node *root)
{
if !(root)
return 0;
if(root->left ==NULL && root->right ==NULL)
return 0;
int bfact =0;
if(root->left !=NULL)
bfact = bfact(root->left) +count(root->left);

if(root->right !=NULL)
bfact = bfact(root->right) + count(root->right);

return bfact;
}

- aka December 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not satisfied with any of the above answer.

Simple logic is find out the number of descendants for left subtree and right subtree seperately and then take the difference.

int numdesc(node * root) //number of descendants
{
	int sum = 0;
	if(root== NULL)
		return 0;
	sum = sum+numdesc(root->left);
	sum = sum+numdesc(root->rght);
	return ++sum;
}

cout <<"Balance Factor of node = " <<(numdesc(root->left) - numdesc(root->rght))<<endl;

- Gajanan December 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question Statement is confusing.if it is like finding bf of root, then it is quite easy
.find height of both sub trees and then find difference between two...

- nagpal_dce August 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Right solution would be-
int BF(Node root, int *sum, int depth) {
if (root==null)
{ *sum = 0;
return 0;
}
int *sum1, *sum2, bf1, bf2, bf;
bf1 = BF(root->left, sum1, depth+1);
bf2 = BF(root->right, sum2, depth+1);
*sum = *sum1 + *sum2 ; // I think as

if(root->left)
*sum++;
if(root->left)
*sum++;

bf = *sum/depth;
return bf + bf1 + bf2;

}

- nagpal_dce August 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// int sum = 0;
// BalanceFactor(&root, 1, &sum);
int BalanceFactor(node* root, int depth, int* sum)
{
	if(root == null)
		return 0;
	
	int l = 0;
	int r = 0;
	int lsum = 0;
	int rsum = 0;
	
	if(root->left != null)
		l = BalanceFactor(root->left, depth+1, &lsum);
	if(root->right != null)
		r = BalanceFactor(root->right, depth+1, &rsum);
	
	int factor = sum\depth;
	sum = sum + lsum + rsum + 1;
	
	return factor;

}

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

// int sum = 0;
// BalanceFactor(&root, 1, &sum);
int BalanceFactor(node* root, int depth, int* sum)
{
	if(root == null)
		return 0;
	
	int l = 0;
	int r = 0;
	int lsum = 0;
	int rsum = 0;
	
	if(root->left != null)
		l = BalanceFactor(root->left, depth+1, &lsum);
	if(root->right != null)
		r = BalanceFactor(root->right, depth+1, &rsum);
	
	int factor = sum\depth;
	sum = sum + lsum + rsum + 1;
	
	return factor;

}

- Anonymous January 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int get_balance_factor(node* ptr)
{
static int sum=0;
int x;
if(ptr==NULL)
{
return 0;
}
if(ptr->left==NULL and ptr->right==NULL)
{
return 1;
}
x=get_balance_factor(ptr->left)+get_balance_factor(ptr->right);
sum=sum+x;
return x;
}

- Anonymous November 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Should it be "return sum"?

- Anonymous November 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Looks like you are just counting the number of nodes.

- Anonymous November 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Where do you use (level_of_node) value?

- Aleksey.M November 29, 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