Microsoft Interview Question for Software Engineer / Developers






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

NODE* reverse(NODE* current)
{
    if(current == NULL)return NULL;
    
    NODE* left = reverse(current->lchild);
    NODE* right = reverse(current->rchild);
    
    if(left != NULL)
    {
         left->lchild = current;
         left->rchild = NULL;
    }
    
    if(right != NULL)
    {
         right->lchild = current;
         right->rchild = NULL;
    }
    
    return current; 
}

- Guess Who?? March 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i did exactly the same way, just a correction
left->ritechild = current;
also u can skip explictly eqauting to NULL as they are altready pointing to NULL.

- aaman.singh85 October 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Small mistake in base condition root's child's pointers shd be to next.

Here is a code ....see if it is correct
void reverseTree(node*root , node* next){
if(root!=NULL){
reverseTree(root->left,root);
reverseTree(root->right,root);
root->left=next;
root->right=next;
}
}

Initial call will be reverseTree(root,NULL)

- Anonymous February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution looks correct.

- Ravi February 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I use only one parameter, instead of two. see if it is correct.

void reverseTree(node*root){
if(root!=NULL){
reverseTree(root->left);
reverseTree(root->right);
root->left->left=root;
root->left->right=root;
root->right->right=root;
root->right->left=root;
}
}

Initial call will be reverseTree(root).

- euv921 March 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is the correct ans

- aammgg March 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Handling the null case for root->left and root->right is missing .When root->left is NULL root->left->left is wrong. The rest of the logic is correct

- pachunuri January 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Modify post-order traversal. Instead of printing the node reverse the pointers:

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

I answered the same but faltered in the code....

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

code above wud crash for leaf nodes atleast.

- Anonymous March 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no it wont. Its like do nothing function for leaf nodes

- cdr May 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if root is leaf ,root.left=null ,
so root.left.left==??

- sunny June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Most of the above solutions are wrong.
Correct solution is below.

void ReverseNodePointers(Node* node, Node* parent)
{
    if(node == null) return;
    Node* leftNode = node->left; // temp child pointer since the node's pointer will be changed.
    Node* rightNode = node->right; // temp child pointer since the node's pointer will be changed.
    node->left = parent;
    node->right = parent;
    traverse(leftNode, node);
    traverse(rightNode, node);
}

ReverseNodePointers(head, head);

- gavinashg March 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C code:

struct tree {
   struct tree* left;
   struct tree* right;
   void*        data;
};

void reverseTree(struct tree* parent, struct tree* node) {
   if(node) {
      reverseTree(node, node->left);
      reverseTree(node, node->right);
      if(parent) {
         if(node == parent->left) {
            node->right = parent;
            parent->left = 0;
         }
         else {
            node->left = parent;
            parent->right = 0;
         }
      }
   } 
}

- Anonymous March 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Initial call would be reverseTree(0, root);

- Anonymous March 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you consider another child pointer of a node? In particular, for the below segment of your code

if(node == parent->left) {
            node->right = parent;
            // what happens to "node->left"
            // should you let it point to parent also,
            // i.e., node->left = parent?
            parent->left = 0;
         }

- Anonymous March 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it should be similar with reversing a singly linked list
Below is the code

listnode *reverseLL(listnode *root) {
    if (!root || !(root->next))
        return root;
    else {
        listnode *tmp = reverseLL(root->next);
        root->next->next = root;
        root->next = NULL;
        return tmp;
    }
}
treenode *reverseBT(treenode *root) {
    if (!root || 
        (root->left == NULL
         && root->right == NULL)) {
        return root;
    } else {
         if (root->left) {
             reverseBT(root->left);
             root->left->left = root;
             root->left->right = root;
             root->left = NULL;
         }
         if (root->right) {
             reverseBT(tmp->right);
             root->right->left = root;
             root->right->right = root;
             root->right = NULL;
         }
    }
}

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

All the approaches one way or the other doing the same thing. But at the end we are left with root node with both of its pointers null thus losing ability to reach any other node in the entire tree. Is this what is expected?

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

The Question is in correct.
Considering a binary Tree.
Parent can have 0 - 2 child nodes
child can have 0 - 1 parent ( 0 in case of root )
How do you reverse this ?

- DarkKnight July 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here is a code ....see if it is correct
void reverseTree(node*root , node* next){
if(root!=NULL){
reverseTree(root->left,root);
reverseTree(root->right,root);
root->left=root;
root->right=root;
}
}

Initial call will be reverseTree(root,NULL)

- Anonymous February 23, 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