## Microsoft Interview Question for Software Engineer / Developers

Country: India
Interview Type: In-Person

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

Here is my implementation on this:

basicalgos.blogspot.com/2012/03/26-convert-binary-search-tree-to-sorted.html

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

/*
Task write the code to find the inorder succesor in binary tree (or BST)
A program to convert an ordered binary tree to doubly link list circular
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left,*right;
};
typedef struct node * Node;

/*
helper function -- given two list nodes, join them
together so the second immediately follow the first.
Sets the .next of the first and the .previous of the second.
*/
static void join(Node a, Node b)
{
if(a)
a->right=b;
if(b)
b->left=a;
}

/*
helper function -- given two circular doubly linked
lists, append them and return the new list.
*/
static Node append(Node a, Node b)
{
if(!a) return b;
if(!b) return a;

Node Alast=a->left;
Node Blast=b->left;

join(Alast,b);
join(Blast,a);

return a;
}

/*
--Recursion--
Given an ordered binary tree, recursively change it into
a circular doubly linked list which is returned.
*/
static Node treeToList(Node root) {

if(!root)
return NULL;

Node alist;
Node blist;
/* recursively solve subtrees -- leap of faith! */
alist=treeToList(root->left);
blist=treeToList(root->right);

/* Make a length-1 list ouf of the root */
root->left=root;
root->right=root;

/* Append everything together in sorted order */
alist=append(alist,root);
alist=append(alist,blist);

}

/* Create a new node */
static Node newNode(int data)
{
Node node=(Node) malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
}

/* Add a new node into a tree */
static void treeInsert(Node* rootRef, int data)
{
Node root = *rootRef;
if (root == NULL) *rootRef = newNode(data);
else
{
if (data <= root->data)
treeInsert(&(root->left), data);
else
treeInsert(&(root->right), data);
}
}

{
printf("hello");
Node current;
while(current)
{
printf("%d",current->data);
current=current->right;
break;
}
}
void printTree(Node root)
{
if(!root)
return;
if(root)
{
printTree(root->left);
printf("%d ",root->data);
printTree(root->right);
}
}
/* Demo that the code works */
int main() {
Node root = NULL;
treeInsert(&root, 4);
treeInsert(&root, 2);
treeInsert(&root, 1);
treeInsert(&root, 3);
treeInsert(&root, 5);
printTree(root);
printf("\nlist\n");
printList(head); /* prints: 1 2 3 4 5 */
int i;
scanf("%d",&i);
return(0);
}

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

Copy paste :D

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

``````//By using inorder traversal,and maintaining a pointer to the previous node in inorder
//we can construct DLL
{
if(root)
{

root->left=(*prev);

if(*prev)
(*prev)->right=root;
else

(*prev)=root;
}
}
node * fun(node *root)
{
node *prev=NULL;
}``````

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

Here is a recursive solution using modified in-order traverse (in C#)
The signature of method is "void BSTToDLL(ref Node root, ref Node head)"
1. if root is null then return
2. call "BSTToDLL(ref root.rchild, ref head)" to convert right sub-tree of root to be DLL and update head
5. call "BSTToDLL(ref root.lchild, ref head)" to convert left sub-tree of root to be DLL and update head
When you finish the routine you will get the head of DLL with head.lchild=null and the tail's rchild=null. So all you need is to use another method to call the routine above and set these two values.

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

``````node ** next = &head;
node** prev =NULL;
bst_to_doubly(node * root)
{
if(root == NULL)
return;
bst_to_doubly(root->left);
node->data = root->data;
node->prev=*prev;
*prev=node;
*next=node;
next=&(node->next);
bst_to_doubly(root->right);

}``````

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

//here root will be BST root node.
//At start just store null into fst (first) and lst (last), and call convertToList (&root,&fst,&lst).
//At the end of execution of convertToList they will have first and last nodes of the DLL respectively.

void convertToList(node **root,node **fst,node **lst)
{
node *temp;
if((*root)->left==NULL && (*root)->right == NULL)
{
*fst = *root;
*lst = *root;
return;
}

if((*root)->left != NULL)
{
convertToList(&((*root)->left),fst,lst);
(*root)->left = *lst;
(*lst)->right = *root;
temp = *fst;
}

if((*root)->right !=NULL)
{
convertToList(&((*root)->right),fst,lst);
(*root)->right = *fst;
(*fst)->left = *root;
}
else
{
*lst = *root;
}

if((*root)->left == NULL)
*fst = *root;
else
*fst = temp;
}

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

Am I missing something here. Isn't this as easy as performing an inorder traversal
And when we "visit" each node. Just add it to the end of the linked list.

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.

### 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.