VMWare Inc Interview Question for Member Technical Staffs


Country: United States
Interview Type: Phone Interview




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

I think the interviewer asked to find the tree diameter. Tree diameter is the max distance between any two nodes in the tree. This can be found in O(n) time.

- Vin May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is tree diameter problem.

Idea is max distance will be in left subtree or in right subtree or it will pass through root.
We can do it recursively.

int height(struct node* root) {
       if(root == NULL) {
             return 0;
       }

       int leftHeight = height(root->left);
       int rightHeight = height(root->right);
       if(leftHeight > rightHeight) {
              return leftHeight + 1;
       } else {
              return rightHeight + 1;
       }
}

int diameter(struct node* root) {
       if(root == NULL) {
              return 0;
       }
       int lHeight = height(root->left);
       int rHeight = height(root->right);

       int lDiameter = diameter(root->left);
       int rDiameter = diameter(root->right);

      return max(lHeight + rHeight + 1, max(lDiameter, rDiameter));
}

- Nitin Gupta May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi Nitin,

Please check in geekforgeeks about tree diameter. your solution is right, but height and diameter can be found in a single pass.

- Vin May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Assuming the binary tree has distinct values and we are given two key between which distance is to be calculated..

int find(node *t,int k,stack s)
{
	if (t->val == k)
		return 1;
	else if (t->l == null && t->r==null)
		return 0;
	else
	{
		int x;
		if (t->l!=null)
		{
			x=find(t->l,k,s);
			if (x>0)
			{
				s.push(t);
				return x+1;
			}
			x=find(t->r,k,s);
			if (x>0)
			{
				s.push(t);
				return x+1;
			}
		}
	}
}

The find function finds the given key and its length and pushes the path onto the stack. The find function is used for both keys. The stack is compared from top to find the common ancestor and twice its distance is subtracted from the sum of two keys to get the distance between them.

- Anonymous May 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

find common ancestor and return common ancestor to node A + common ancestor to node B distance

- sam May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Question says binary tree, not BST, so maybe its not organized as you assumed? or maybe it was transcribed wrong.

In any case, even if a BST, when finding the common ancestor, consider the case where the one of the nodes is an ancestor to the other one, for instance, a parent/child relationship.

- dn May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@dn: For doing what sam said, tree need not be binary search tree because
1) For finding common ancestor tree need not be BST
2) After find the common ancestor we can use stack for finding the distances beteen the common ancestor and each of the node and add them.

- dadakhalandhar May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@dadakhalandhar. So assuming question is find the lowest common ancestor of a binary tree, what if we DFS the tree, recording path as we go along, and search for both nodes. Upon finding both nodes, then compare the paths of the two, starting from the root node and on down. The root node must match but eventually the paths will diverge. Where the paths diverge is the LCA. Is that what you mean by using the stack?

- dn May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let us consider the least common ancestor of D and G be F. And corresponding sub tree be

F
          /      \
        B        G
      /    \       /   \ 
     A   D     I     C

Do preorder traversal of left sub-tree of F. while pushing the elements into stack while entering the sub-sub-tree and poping while leaving. When we find D, count the number of entries in the stack. This will give the distance between the left element to the common ancestor.
Similarly we can do for right element.

- dadakhalandhar May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find the deepest node on each side of the root. That should be the longest possible path between two nodes. Please let me know if im missing something.

- arun_lisieux May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about

root 
     /     \
    / \
   /   \
  /     \
 /       \

Clearly your method does not work.

- ao May 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ao
True, initial solution will not work.
How about doing this recursively for each node, storing the sum of depths of left and right subtree along with the node and then searching for the node with highest value.

- arun_lisieux May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Guys, What's the fuss ??

Check my solution below...upvote it.

:-)

- Nitin Gupta May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution would be:

Run Postorder on binary tree...
take a static variable...
for each node :
if it is leaf node return 0 + 1(for branch connecting to parent)
else maximum of {left tree value, right tree value} + 1
(if either of child tree is not present take it as 0)......

at each node calculate current_max = left tree value + right tree value
if this is bigger than our static variable update our static varaible = current_max....

At end of the process our static variable will contain maximum distance between any of two nodes in our binary tree... :)

- coding.arya May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And this problem is also known as diameter of a tree... :)

- coding.arya May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a question of finding maxDepth in a tree.

- Anil May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a tree walk while keeping track of the current path using a list, by appending the current node to the list when starting a new recursive call, and popping it when returning.
During the tree walk, look for for two nodes, and when each of them is found, copy the current path and store it for later.
At the end of the tree walk, copmpare the two paths node by node, to find the last common ancesotr. The ditance would be the sum of the rest of the nodes after the common ancestor in the two paths.

- gen-y-s May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2logn

- Amartya May 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find lowest common ancestor, then find distance from both

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

The answer to the above question would be to find the diameter of the given tree
Here's the code which works in O(n) :

int diameterOpt(struct node *root, int* height)
{
/* lh --> Height of left subtree
rh --> Height of right subtree */
int lh = 0, rh = 0;

/* ldiameter --> diameter of left subtree
rdiameter --> Diameter of right subtree */
int ldiameter = 0, rdiameter = 0;

if(root == NULL)
{
*height = 0;
return 0; /* diameter is also 0 */
}

/* Get the heights of left and right subtrees in lh and rh
And store the returned values in ldiameter and ldiameter */
ldiameter = diameterOpt(root->left, &lh);
rdiameter = diameterOpt(root->right, &rh);

/* Height of current node is max of heights of left and
right subtrees plus 1*/
*height = max(lh, rh) + 1;

return max(lh + rh + 1, max(ldiameter, rdiameter));
}

- Saurabh Singhal August 17, 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