Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Do an in-order traversal of the binary tree, at each node joining the linked list resulting from the left and right children.

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

public static Node[] createLinkedList(Node root) {
    if (root == null) return new Node[] {null, null};
    Node[] result = new Node[2];
    if (root.leftChild != null) {
        Node[] leftList = createLinkedList(root.leftChild);
        root.previous = leftList[1];
        root.previous.next = root;
        result[0] = leftList[0];
    } else result[0] = root;
    if (root.rightChild != null) {
        Node[] rightList = createLinkedList(root.rightChild);
        root.next = rightList[0];
        root.next.previous = root;
        result[1] = rightList[1];
    } else result[1] = root;
    return result;
}

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

nice job

- Anonymous February 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

just use the morris inorder traversal :)

- geeks October 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node BSTtoDL(Node nd, Node lastPrintedNode){

if(nd.getLeft()!=null)
lastPrintedNode = BSTtoDL(nd.getLeft(),lastPrintedNode);

if(lastPrintedNode == null)
{
lastPrintedNode = nd;
nd.setLeft(null);
}
else
{
lastPrintedNode.setRight(nd);
nd.setLeft(lastPrintedNode);
}
if(nd.getRight()!=null)
lastPrintedNode = BSTtoDL(nd.getRight(), lastPrintedNode);

return lastPrintedNode; 
}

- ishant agarwal November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

F(n, b)
	if(!n->r && !n->l)
		return n
	nl = F(n->l, 0)
	nr = F(n->r, 1)
	n->l = nl
	nl->r = n
	n->r = nr
	nr->l = n
	if(b)
		return nl
	else
		return nr

- Prateek Caire November 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we use recursion ??

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

#include<iostream>

using namespace std;

class TreeNode
{
public:
int info;
TreeNode* right;
TreeNode* left;
//TreeNode* parent;
TreeNode()
{
info = 0;
right = left = NULL;
}

TreeNode(int info)
{
this -> info = info;
right = left = NULL;
}
};

class ListNode
{
public:
int info;
ListNode* next;
ListNode()
{
info = 0;
next = NULL;
}

ListNode(int info)
{
this -> info = info;
next = NULL;
}
};

TreeNode* root = NULL;
ListNode* start;
ListNode* end;

void addTreeNode(TreeNode*);
void convertTreeToList(TreeNode*);
void printList(ListNode*);
int findDepth(TreeNode*);

int main()
{
start = end = new ListNode(0);
char choice = 'y';
int info;
while(choice == 'y')
{
cout<<"Enter the info of new node:\n";
cin>>info;
addTreeNode(new TreeNode(info));
cout<<"Want to add a new node to the tree?(y/n)\n";
cin>>choice;
}

cout<<"Depth of the tree is: "<<findDepth(root);

cout<<"Converting the tree into a doubly linked list....\n";

convertTreeToList(root);
printList(start->next);
cin>>choice;
return 0;
}



void addTreeNode(TreeNode* node)
{
if(!root)
{
root = node;
}
else
{
TreeNode* currRoot = root;
while(1)
{
if(node -> info >= currRoot -> info)
{
if(!currRoot -> right)
{
currRoot -> right = node;
break;
}
else
{
currRoot = currRoot -> right;
}
}
else
{
if(!currRoot -> left)
{
currRoot -> left = node;
break;
}
else
{
currRoot = currRoot -> left;
}
}
}
}
}

void convertTreeToList(TreeNode* node)
{
if(node -> left != NULL)
{
convertTreeToList(node -> left);
}

end ->next = new ListNode(node -> info);
end = end -> next;
end -> next = start;



if(node -> right != NULL)
{
convertTreeToList(node -> right);
}

}


void printList(ListNode* start)
{
while(start != ::start)
{
cout<<start->info<<" -> ";
start = start -> next;
}
cout<<"x";
}

int findDepth(TreeNode* node)
{
if(!node)
{
return 0;
}
else
{
return (max(findDepth(node -> left), findDepth(node -> right)) + 1);
}
}

- deepti.3feb June 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

follow this link.............

programmingwar.blogspot.com/2011_10_01_archive.html

- hemant October 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Spam

- King@Work October 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Converting to singly linked list (can easily be modified to dll).
-1. r = root
0. Until node r has no children
1. Keep rotating right until there is no left child or r
2. set r = r->right;
3. go to step 0

TNode* Tree::rr(TNode* r)
{
TNode* A = r;
TNode* B = r->left;
TNode* Br = B->right;

B->right = A;
A->left = Br;

return B;
}

void Tree::convertToLL()
{
TNode* r = root;
TNode* p = NULL; // parent

while ( r->left != NULL || r->right != NULL )
{
while ( r->left != NULL )
{
r = rr(r);
if ( p != NULL )
p->right = r;
else
root = r;
}
p = r;
r = r->right;
}
}

- Coderilla November 12, 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