Microsoft Interview Question for Software Engineer / Developers


Country: United States




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

Do post order traversal and perform LL and RR rotation around each '*' node.

- Anonymous May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

Inorder traversal of the wrong tree will give us back the infix expression, convert that to a post fix expression using the correct operator precedence, and then parse the postfix expression to get the correct tree.

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

I hate that evil dwarf

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

Sounds like homework/programming puzzle.

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

This comment has been deleted.

- Administrator May 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

according to given constraints we get (a+b)*((c+d)-e)

Constructing parse tree for this :-

*
/ \
+ -
/ \ / \
a b + e
/ \
c d

- Mahendra Pratap May 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

according to given constraints we get (a+b)*((c+d)-e)

Constructing parse tree for this :-

*
/ \
+ -
/ \ / \
a b + e
/ \
c d

- Mahendra Pratap May 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can do first Right rotation around * and then Left rotation around the * node and you will get the correct result.

search wiki for the tree rotation.

- Raj Jaiswal May 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

rotation is a good idea !

- linda June 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

First Right Rotation around node * and then Left Rotation around node *
* + +
/ \ / \ / \
+ + Left Rotation a * Right Rotation a +
/ \ / \ =====> / \ =====> / \
a b c - b + * -
/ \ / \ / \ / \
e f c - b c e f
/ \
e f

- Raj Jaiswal May 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First Right Rotation around node * and then Left Rotation around node *
        *                                                           +                                                     +
    /       \                                                    /       \                                                /     \
   +           +             Left Rotation          a        *          Right Rotation           a      +
 /   \         /   \               =====>                       /     \             =====>                     /      \
a     b    c     -                                                 b     +                                             *         -
                   /  \                                                     /     \                                        /    \     /   \
                 e    f                                                  c      -                                       b   c   e    f
                                                                                /    \
                                                                               e    f

- Raj Jaiswal May 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey raj, I am unable to undersand your solution can you please explain me in more detail

- Srishti June 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I wrote a c++ version, rotate twice for each *

#include <iostream>
#include <assert.h>
using namespace std;



struct Node;

typedef struct Node * pN;

struct Node {
char data;
pN left;
pN right;
};


void print_tree (pN rootN)
{
if(rootN==NULL) return;
else {
print_tree(rootN->left);
cout<<rootN->data<<" ";
print_tree(rootN->right);
return;
}
}

//rotate right, return the new root
pN rotate_right(pN rootN) {
cout<<"rotate right"<<endl;
if (rootN==NULL || rootN->right == NULL || rootN->left == NULL ) return rootN;
else {
pN lN = rootN->left;// pN rN = rootN->right;
if(lN->left==NULL || lN->right==NULL) //this is just for our case, leaf node cannot be empty -- because there is no such thing as 'NULL+b'
return rootN;
else {
rootN->left = lN->right;
lN->right = rootN;
return lN;
}
}
}

//rotate left, return the new root
pN rotate_left(pN rootN) {
cout<<"rotate left"<<endl;
if (rootN==NULL || rootN->right == NULL || rootN->left == NULL ) return rootN;
else {
// pN lN = rootN->left;
pN rN = rootN->right;
if(rN->left==NULL || rN->right==NULL) //this is just for our case, leaf node cannot be empty -- because there is no such thing as 'NULL+b'
return rootN;
else {
rootN->right = rN->left;
rN->left = rootN;
return rN;
}
}
}

//correct the tree by rotating, return the new root
pN myf (pN rootN) {
if (rootN==NULL) return NULL;
else {
cout<<"work on node "<<rootN->data<<endl;
pN newleft=NULL; pN newright=NULL;
if (rootN->left) {
newleft = myf(rootN->left);
cout<<"new left subtree: "<<endl;
print_tree(newleft); cout<<endl; }
if (rootN->right) {
newright = myf(rootN->right);
cout<<"new right subtree: "<<endl;
print_tree(newright); cout<<endl; }
rootN->left = newleft;
rootN->right = newright;
if (rootN->data == '*' )
{
cout<<"now rotate node * "<<endl;
assert(newleft); assert(newright);
pN newroot_rr = rotate_right(rootN);
pN newroot_rl = rotate_left(newroot_rr);
return newroot_rl;
}
else return rootN;
}
}


void init_node (pN newnode, char newdata, pN l, pN r) {
newnode->data = newdata;
newnode->left = l;
newnode->right = r;
return;
}


int main() {
pN mynode1 = (pN)calloc(1,sizeof(Node));
init_node(mynode1,'a',NULL,NULL);
pN mynode2 = (pN)calloc(1,sizeof(Node));
init_node(mynode2,'b',NULL,NULL);
pN mynode3 = (pN)calloc(1,sizeof(Node));
init_node(mynode3,'c',NULL,NULL);
pN mynode4 = (pN)calloc(1,sizeof(Node));
init_node(mynode4,'d',NULL,NULL);
pN mynode5 = (pN)calloc(1,sizeof(Node));
init_node(mynode5,'e',NULL,NULL);

pN mynode6 = (pN)calloc(1,sizeof(Node)); //a+b
init_node(mynode6,'+',mynode1,mynode2);
pN mynode7 = (pN)calloc(1,sizeof(Node)); //d-e
init_node(mynode7,'-',mynode4,mynode5);
pN mynode8 = (pN)calloc(1,sizeof(Node)); //c+(d-e)
init_node(mynode8,'+',mynode3,mynode7);
pN mynode9 = (pN)calloc(1,sizeof(Node));
init_node(mynode9,'*',mynode6,mynode8);

cout<<"check original tree :"<<endl;
print_tree(mynode9);
pN newroot = myf(mynode9);
cout<<"after rotations :"<<endl;
print_tree(newroot);

return 0;
}

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

This tree is just an example..wat if we have any other tree..will this method work??

- Anonymous July 04, 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