Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
#include <iostream>
char pre[7];
char in[7];
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;
}
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
In order/pre order should be fairly straightforward
- Anon August 24, 2012IN: 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"