Microsoft Interview Question for Software Engineer in Tests






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

// My code

postorder(root,gparent,parent){
    if(root==null)return;
    root->granparent=gparent;
    postorder(root->left,parent,root);
    postorder(root->right,parent,root);
}
call postorder(root,null,null)

- Frodo Baggins August 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Parent will always be null here

- ibnipun10 August 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am sorry, you are right....:)

- ibnipun10 August 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud work

- santhosh August 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome!!

- coder September 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good and clean solution!

- Ricardo Neto November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

superb!!!

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

Good. Only one thing here is that this is PREorder-action traversal but not a postorder as you named it (postorder(root,gparent,parent)).

- Aleksey.M November 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My first idea:
1. complete the rest of the tree with dummy nodes until it's a complete binary tree.
2. do a level order and save it to an array.
3. use the idea from binary heap to get the grand parent.

- S August 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My second Idea :)

postorder(root,gparent,count){
  if(root==null) return;
  if(count==0){
      root.granparent = gparent;
      count=2;
  } 
  postorder(root.left,root,count-1);
  postorder(root.right,root,count-1);
}
call postorder(root,null,2);

- MaYanK August 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

:( above code is wrong see Frodo's code below.

- MaYanK August 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@MaYanK , I think you are assigning parent instead of grand parent here .
Every time you are calling postorder(root,gparent,count) ->
1. either gparent is null
2. or gparent->left=root or gparent->right=root
so , gparent is not the grand parent here , its parent .

One more thing , it will only set the gparent for the nodes where cout = 0 .
so , suppose we are at node current_node ,
then postorder(current_node->left,..) and postorder(current_node->right,..) will be called with count = 1 , and so the gparent for those nodes will not be assigned .

- Frodo Baggins August 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Frodo Baggins
You are right. :)

- MaYanK August 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can do level order traversal keeping track of lastnode traversed and parentOfLastNodeTravesed.

before dequeuing the next level element enqueue NULL as indication of next level and parentOfLastNodeTravesed traversed.

- Sravan August 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Frodo, great solution.

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

thank you for your solution frodo :)

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

void assign_gp(node * root, node * parent)
{
if(root)
{ if(root->left)
{ root->left->gparent=parent;
assign_gp(root->left,root);
}
if(root->right)
{ root->right->gparent=parent;
assign_gp(root->right,root);
}
}
}

- harsh1690 August 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice one !!

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

void assign_gp(node * root, node * parent)
{
if(root)
{ if(root->left)
{ root->left->gparent=parent;
assign_gp(root->left,root);
}
if(root->right)
{ root->right->gparent=parent;
assign_gp(root->right,root);
}
}
}
.
.
.
main()
{ root->gpearent=NULL;
assign_gp(root, NULL);
}

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

void SetGrandParent(Node root) {
SetGrandParent(null, null, root);
}

void SetGrandParent(Node grandParent, Node parent, Node root) {

node.grandparent = grandparent;
if (root.left != null) {
SetGrandParent(parent, root, root.left);
}

if (root.right !=null) {
SetGrandParent(parent, root, root.right);
}
}

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

public static void AssignGrandParent(TreeNode root)
{
if (root == null)
return;

if (root.Parent != null)
root.GrandParent = root.Parent.Parent;

AssignGrandParent(root.LeftNode);
AssignGrandParent(root.RightNode);
}

- Venky August 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Node structure does not have Parent field.

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

I think if we have both parent and grandparent in the node implementation then it will quite easy implementation.

- O(?) September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my C# implementation

static void SetGrandParent(Node n, Node parent, Node GrandParent)
        {
            
            if (GrandParent != null)
                n.GrandParent = GrandParent;
            if (parent != null)
            {
                if (n.Left != null)
                    SetGrandParent(n.Left, n, parent);
                if (n.Right != null)
                    SetGrandParent(n.Right, n, parent);
            }
            else
            {
                if (n.Left != null)
                    SetGrandParent(n.Left, n, null);
                if (n.Right != null)
                    SetGrandParent(n.Right, n, null);
            }
                
        }

and first Call Will be

SetGrandParent(tree, null, null);

- Sameh.S.Hegazy September 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am sure next five lines could have been enough.

n.GrandParent = GrandParent;
if (n.Left != null)
SetGrandParent(n.Left, n, parent);
if (n.Right != null)
SetGrandParent(n.Right, n, parent);

- OPTIMIZE DUDE September 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes , right no need for the extra code ,
thanks for updating me :)

- Sameh.S.Hegazy September 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how to make initial call with only those five lines.

- Paul October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think OPTIMIZE DUDE means that the code with the method has to be minimized , so the function and the first call will still be the same , i traced it and the same output was generated

- Sameh.S.Hegazy October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

main(){
assigngrandparent(root->left,root);
assigngrandparent(root->right,root);
}

assigngrandparent(node n,node gpn)
{
if(gpn!=null)
{node->left->gp=gpn; // if there is a left child for node
node->right->gp=gpn; // if there is a right child for node
}
assigngrandparent(node->left,node);// if there is a left child for node
assigngrandparent(node->right,node);// if there is a right child for node
}

- Anonymous September 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No checks whether n->left or n->right is null. BIG MISTAKE
Check for gpn is useless.

- CHECK FOR NULL September 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, Simplest approach...

Push(root)
while(isempty() == FALSE)
{
node = pop();
if(node != NULL)
{
if (node->left != NULL)
{
node->left->garandparent = node;
push(node->left)
}
else
push (NULL);
if (node->right != NULL)
{
node->right->garandparent = node;
push(node->right);
}
else
push (NULL);
}
}

And there u go dude...

- Prashant September 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

U are only assigning the parents to the nodes.
At least check your solutions before putting here.

- Where to go dude September 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void set_grand(node* n, node *p)
{
if(!n)
return;
if(n->left)
{
n->left->gp = p;
set_grand(n->left, n);
}
if(n->right)
{
n->right->gp = p;
set_grand(n->right, n);
}
}

set_grand(root, 0);

- i.shahin September 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simplest Solution:
void assignGrandParent(Node* curr, Node* parent, Node* gparent)
{
if(curr == NULL)
return;
curr->grandparent = gparent;
assignGrandParent(curr->left, curr, parent);
assignGrandParent(curr->right, curr, parent);
}
Is there anything wrong with this approach:

- Vinay October 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class TreeNode
        {
            public int Data { get; set; }
            public TreeNode Left { get; set; }
            public TreeNode Right { get; set; }
            public TreeNode GrandParent { get; set; }

            public TreeNode(int data)
            {
                this.Data = data;
                this.Left = null;
                this.Right = null;
                this.GrandParent = null;
            }
               
        public void SetGrandParent(TreeNode root)
        {
            Queue<TreeNode> q = new Queue<TreeNode>();
            q.Enqueue(root);
            TreeNode current = null;
            while (q.Count > 0)
            {
                current = q.Dequeue();
                if (current.Left != null)
                {
                    q.Enqueue(current.Left);
                    CheckChildren(current.Left, current);
                }
                if (current.Right != null)
                {
                    q.Enqueue(current.Right);
                    CheckChildren(current.Right, current);
                }
            }
        }

        private void CheckChildren(TreeNode parent, TreeNode grandParent)
        {
            if (parent.Left != null)
            {
                parent.Left.GrandParent = grandParent;
            }
            if (parent.Right != null)
            {
                parent.Right.GrandParent = grandParent;
            }
        }

        public void DisplayFamily(TreeNode node)
        {
            if (node == null)
            {
                return;
            }
            DispayFamily(node.Left);
            Console.WriteLine(" Grand father of {0} is : {1}", node.Data, (node.GrandParent == null) ? "Null" : node.GrandParent.Data.ToString());
            DispayFamily(node.Right);
        }
    }

- Mi Jalgaonkar June 18, 2011 | 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