## Microsoft Interview Question for Software Engineer / Developers

• 0

Country: India
Interview Type: Written Test

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

In order/pre order should be fairly straightforward
IN: L, R, print
pre: print, L, R

now recomposing the tree is easier explained than written flat out
lets say we have a tree like such
-------a---
---b-----c--
-d--e-------f-
g--------------

inorder: gdbeacf
preorder: abdgecf

we know the first thing in preorder is the root, so we'll split that in both orders
inorder: gdbe|a|cf
preorder: |a|bdgecf

now in inorder, we have 2 halves, which correspond to the left and right subtrees of a (gdbe and cf). we can find the same set of matching elements in preorder (bgde and cf)

we repeat the same thing we did for a for each subtree(since first thing in each subtree should be the root node of each subtree), getting these splits
left
in:gd|b|e
pre:|b|gde
(b is root node, left subtree has gd, right has e)

right
in: |c|f
pre: |c|f
(c is root node, has no left subtree, right subtree (child) is f)

repeat this for any remaining subtrees with more than 1 element left (in this case, gd subtree)

in:|g|d
pre:|g|d
we see that g is the root node, d is the right child of g

so now we know our root is a, with a left child b, and right child c
b has left child d, right child e, and c has right child f
d has right child g

thus our tree has been reconstructed

in short this is the process
1. take first element in preorder (this is the root)
2. split inorder by that element (these are the decendents of that root)
3. split preorder by the same groups of elements that inorder has split (subtrees)
4. repeat with the "subtrees"

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

``````#include <iostream>

char pre;
char in;
int cnt = 0;

struct node
{
char data;
node* left;
node* right;
node(int eData, node* eLeft, node* eRight)
{
data = eData;
left = eLeft;
right = eRight;
}
};

void preOrder(node* n)
{
if (!n)
return;

pre[cnt++] = n->data;
preOrder(n->left);
preOrder(n->right);
};

void inOrder(node* n)
{
if (!n)
return;

inOrder(n->left);
in[cnt++] = n->data;
inOrder(n->right);
};

node* restoreTree(int left, int right, int inLeft, int inRight)
{
if (left < right)
{
char curData = pre[left];
int i = inLeft;
for (; i <= inRight; i++)
{
if (in[i] == curData)
break;
}

int num = i - inLeft;
node* curNode = new node(curData, NULL, NULL);
curNode->left = restoreTree(left + 1, left + num, inLeft, i - 1);
curNode->right = restoreTree(left + num + 1, right, i + 1, inRight);
return curNode;
}
else if (left == right)
return new node(pre[left], NULL, NULL);

};

int main()
{
node* n6 = new node('G', NULL, NULL);
node* n5 = new node('D', n6, NULL);
node* n4 = new node('E', NULL, NULL);
node* n3 = new node('B', n5, n4);
node* n2 = new node('F', NULL, NULL);
node* n1 = new node('C', n2, NULL);
node* n0 = new node('A', n3, n1);

cnt = 0;
preOrder(n0);

cnt = 0;
inOrder(n0);

node* root = restoreTree(0, 6, 0, 6);

return 0;
}``````

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

test cases are asked rather than code..

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

wonderful code..thanks..:)

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

what does he mean by testing? dows he wanted to construct the tree back?

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

There are multiple layers of testing
1. Test Layer 1, Test the generation of pre order string
2. Test Layer 2, Test the generation of inorder string
3. Test layer 3, Given Inorder and Preorder string verify that tree constructed are right
Once these three layers are tested, we can test E2E,
We can maintain a test data struct, with possible output

struct TestData {
BTNode *root; // represents the tree
char *inorderStr;
char *preorderStr;
};

Create test cases and store them in an array or list, and test all the three layer

For End to end testing you might have to implement IsBTreeSame, that will check for parity of the tree sent by person 1 and recieved by person 2

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.