VMWare Inc Interview Question for Software Engineer / Developers






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

I doubt they will give you the parent pointers, if parent pointers not given i think its merely asking you to find the LCA (lowest Common Ancestor)

- hary February 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the address of the head node is not given then this problem is quite tricky.
Other wise we store the address of the nodes in the path from the root to the first node. Then we go from the root to the 2 node. At each node we encounter we check the earlier stored path ....

- abhimanipal March 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can do something like this

while (LastNode in pathp not present in pathq &&
       LastNode in pathq not present in pathp) {
  p = p->parent();
  q = q->parent();
  Append p in the list pathp
  Append q in the list pathq
}

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

We can do something like this

while (LastNode in pathp not present in pathq &&
       LastNode in pathq not present in pathp) {
  p = p->parent();
  q = q->parent();
  Append p in the list pathp
  Append q in the list pathq
}
Common Ancestor = last node whichever satisfied the criteria to break out of the loop

- Ash March 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Checking the paths list again and again for each time you add a parent node does not seem to be efficient.

- SS September 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For a tree as root node is global i think they provide root node also .. if it is the case ..

do this ..

do {
if (node1 == root || node2 == root) {
printf("Invalid i/p");
exit(1);
}
p1=getparent(node1);
p2=getparent(node2);
while (p1 != p2 || p1 != NULL || p2 != NULL) {
p1=getparent(node1);
p2=getparent(node2);
}
if (p1 != NULL || p2 != NULL) {
printf("Invalid i/p");
exit(1);
}
else
print("p1 or p2 which is needed parent");
}


getparent can be a simple recursive function to get each nodes parent. :)

- Div March 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you could do a bfs to get the height and parent of each node(assuming ur given the root ofcourse), then jump to parents of the node which is farther from the root till they're at the same level(you might just encounter the other node as the predecessor now), then race towards the root and check for equality. Space complexity is bad due to the storage for depth and parent, but is faster now.

- howzzat? April 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this seems to be better.

- sameer December 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hey can be Inorder successor or predecessor of particular pointer would be a nearest parent to the pointer with left and right subtree condition

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

hey can be Inorder successor or predecessor of particular pointer would be a nearest parent to the pointer with left and right subtree condition
Do correct me if i am wrong

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

bool Find(node *NodeToBeFound, node *StartNode)
{
    if(NodeToBeFound->data == StartNode->data)
        return 1;
    if(Find(NodeToBeFound, StartNode->left) == 1)
        return 1;
    if(Find(NodeToBeFound, StartNode->right) == 1)
        return 1;
}

   node *Parent = FirstNode;
   found = 0;
   while(found == 0)
   {
       Parent = Parent->parent;      
       found = Find(Parent, SecondNode);       
   }
   
   Parent is the required common nearest parent

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

Node FindNearestParent(Node a,Node b)
{
  List<Node> ParentList = new ArrayList<Node>();
  while(a.parent!=null)
  {
   ParentList.add(a.parent);
   a=a.parent;
  
  }
  while(b.parent!=null)
  {
   if(ParentList.contains(b.parent))
   {
        return b.parent;
      
   }
   b=b.parent;
  
  
  
  }

- dushy February 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Assuming node structure has a parent pointer */

Node* FindCommonAncestor (node *node1, node *node2) {
        while ( node1 != node2 ) {
                if (node1 != NULL) node1 = node1->parent;

                if (node2 != NULL) node2 = node2->parent;
        }

        return node1;
}

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

its same as given two link list that merge at a point... find the merging point...
logic:
1) Start pointer1 and count number of nodes to root (len1)
2) Start pointer2 and count number of nodes to root (len2)
3) Max(len1, len2) say len1
4) Move pointer 1 by difference of len1-len2
5) then move both of them together untill you have ptr1!=ptr2

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

you can also think problem as finding merging node in given two link list and you need to find merging node of this two list which will be actually nearest parent of both head node (given two node of tree). you can think root node as a last node of both list since root->parent=NULL

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

Assuming, every node of tree has parent field apart from tradition left, right and data field, traverse the tree upwards using parent field.

Let p and q be pointers to any two nodes.

then

while(p->parent != q->parent)
     {
           p = p->parent;
           q = q->parent;
     }   
     NODE *t = p;

Node t is the nearest parent to p and q.

- cirus February 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong.

- Mahesh February 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very wrong. You are assuming the two nodes are at the same height.

- Anonymous June 16, 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