Microsoft Interview Question for Software Engineer / Developers

Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
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

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
(b is root node, left subtree has gd, right has e)

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)

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"

- Anon August 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
of 2 vote

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

	pre[cnt++] = n->data;

void inOrder(node* n)
	if (!n)
	in[cnt++] = n->data;

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)

		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;

	cnt = 0;

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

	return 0;

- hao.liu0708 August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 votes

test cases are asked rather than code..

- rockstar August 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
of 0 vote

wonderful code..thanks..:)

- Anonymous August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 votes

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

- t.deepak.iitg September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
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

- asahu September 07, 2012 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


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


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