Interview Question for Software Engineer / Developers






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

no... in addition to that its require one of preorder or postorder

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

or even levelorder

- Kishore Jinka August 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes i think

- getjar.com/todotasklist my android app August 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can with
inorder & preorder
or
inorder & postorder

but not with inorder & levelorder

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

We can construct a tree with inorder & levelorder...this is a standard google interview question. The solution is as follows.

/**
Consider for example
In-order: 3 2 1 4 5 7 6
Level-order: 4 2 7 3 1 5 6

root = levelorder[0]#set root to first element in levelorder
subIn1, subIn2 = partition(inorder, levelorder[0]) #partition inorder based on root
subLevel1 = extract(levelOrder, subIn1)#remove elements in level order not in subIn1
subLevel2 = extract(levelOrder, subIn2)#remove elements in level order not in subIn2
root->left = f(subIn1, subLevel1)
root->right = f(subIn2, subLevel2)
return root


*/
public static BTNode buildBTFromInOrderAndLevelOrder(Object[] inOrderData, Object[] levelOrderData){
if(inOrderData.length != levelOrderData.length){
System.out.println("Inconsistent data: No.of BTNodes in InOrder & LevelOrder don't match");
return null;
}
//the root node is the first element in the LevelOrder data
BTNode rootBTNode = new BTNode();
Object rootData = levelOrderData[0];
rootBTNode.setData(rootData);

//base condition
if(inOrderData.length == 1){
return rootBTNode;
}

int inOrderRootIndex = Arrays.binarySearch(inOrderData, rootData);
Object[] leftSubInOrderData = Arrays.copyOfRange(inOrderData, 0, inOrderRootIndex);
List levelOrderList = new ArrayList(Arrays.asList(levelOrderData));
levelOrderList.retainAll(Arrays.asList(leftSubInOrderData));
Object[] leftSubLevelOrderData = levelOrderList.toArray();

Object[] rightSubInOrderData = Arrays.copyOfRange(inOrderData, inOrderRootIndex+1, inOrderData.length);
levelOrderList = new ArrayList(Arrays.asList(levelOrderData));
levelOrderList.retainAll(Arrays.asList(rightSubInOrderData));
Object[] rightSubLevelOrderData = levelOrderList.toArray();

rootBTNode.setLeft(buildBTFromInOrderAndLevelOrder(leftSubInOrderData,leftSubLevelOrderData));
rootBTNode.setRight(buildBTFromInOrderAndLevelOrder(rightSubInOrderData,rightSubLevelOrderData));
//the tree is constructed now in a recursive way
return rootBTNode;
}

- Kishore Jinka August 30, 2011 | Flag


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