Microsoft Interview Question for Software Engineer in Tests






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

The number of nodes per level contributes to the density of the tree. Intuitively, density is a measure of the size of a tree (number of nodes) relative to the height of the tree.

so density can be calculated as- d=(size)/(height)

float density(struct node*root)
{
return(size(root)/height(root));
}

//calculating size
int size(struct node*root)
{
if(root==NULL) return 0;
else
return(size(root->left)+size(root->right)+1);
}
//calculating height
int height(struct node* root)
{
int l,r;
if(root==NULL) return 0;
else
{
l=height(root->left);
r=height(root->right);
if(l>r)return ++l;
else return ++r;
}
}

- Ali_BABA January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I believe you can find the number of nodes in the tree while calculating the height.

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

@Rayden Can u tell me how i can count nodes in the tree.

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

Rayden, your belief is wrong, seems like you dont understand recursion

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

I think Rayden is correct we can find number node while we getting the height. just passing ref int count and count ++

- Kar February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ker Please tell me How ? How will u do this please write recursive code for this

- anonymous February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Instead of passing variable just use one static variable and increase it

- Santosh March 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

HI all
I think it’s a application of BFS .
Please find my algo n let me know if its fine.

1. Apply BFS on root.
2. While inserting nodes into queue give levels to all the nodes which are adjacent to the node inserted
Root ->level – 0
Root left, right child -level – 1 etc.
3. Count the nodes as there are many ways
[e.g. we can use count variable and increment whenever inserting in queue or whenever that node is coming out of the queue as each node will be inserted exactly once in the tree.]
4. maximum level no will give us height , count var will give no of nodes.
5. Calculate the density…

Complexity of algo O(N)…

- joker February 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int height=0,nodes=0;
Call(root,&height,&nodes);

void density(struct node *root,int *height,int *nodes)
{
        if(!root)
        {
                *height=0;
                *nodes=0;
                return;
        }
        int lh=0,rh=0,ln=0,rn=0;
        density(root->left,&lh,&ln);
        density(root->right,&rh,&rn);
        *height=max(lh,rh)+1;
        *nodes=ln+rn+1;
        return;
}

Density=nodes/height

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

to compute height and width together,
Assuming , Tree is made of Node.

float density(Node* t){
  if (!t)
	return Nan;  
  Queue<Node*> Q[2];
  bool qid = 0;
  Q[qid].push(t);
  uint w = 1;
  uint h = 0;
  float den = 0;
  while (!Q[qid].empty)
  {
    ++h;
    uint wt = 0;
    while(!Q[qid].empty)
       {
          Node* L = Q[qid].pop();  
          if (L -> left)
          {
            Q[!qid].push(L->left); ++wt;
          }
          if (L -> right)
          {
            Q[!qid].push(L->right); ++wt;
          }          
       }
	if (w < wt) 
		w = wt;
    qid = !qid;   
  }
  den = w / h;
  return den;
}

It was a bit difficult to write algorithm, so I wrote code, it should be quite straight forward to read it. The code has not been tested, It is more a representative of my idea of computing height and width together.

- abhishekatuw January 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for this solution.
The time complexity of this algorithm is O(N^2) so I think its very difficult as compare to above solution

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

The complexity is actually O(n) because every node is visited only once. This is a standard breadth-first traversal algorithm.

- misha March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

PS: but why complicate things with the 2 queues instead of just using one?

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

int s[100];
int leveldensity(tree *p,int level)
{
if(p!=NULL)
{s[level]++;
levedensity(p->left,level+1);
leveldensity(p->right,level+1);

}

}

Consider the following tree
A level 0 : s[0] =1
/ \
B C level 1 : s[1]=2
/ \ / \
D E F G level 2 : s[2] =4

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

I don't think density should be defined as ratio of no. of nodes to height. I think density should be defined as #nodes in tree / #total no. of nodes in complete binary tree of that height

- JustMe June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

WTF?

Define "density of a tree".

- Anonymous January 24, 2012 | 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