Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Essentially the idea is the same as what candis said. Here is the code for same which I implemented in Java.
I Since the inorder is contiguous, the trick here is to find the start and end elements in the Pre-Order which are to the left of the current root node in the in-order sequence and pass it recursively to construct left and right subtrees

public void reconstructInOrderPreOrder(ArrayList<Integer> inOrder, ArrayList<Integer> preOrder) throws ConstraintViolationException {
		if (inOrder.isEmpty() || preOrder.isEmpty()) {
			throw new ConstraintViolationException("Invalid Input Arrays");
		}
		
		root = reconstructInOrderPreOrder(root, inOrder, preOrder);
	}

        public int findIndexInOrder(List<Integer> inOrder, Integer key) {
		return inOrder.indexOf(key);
	}

       private TreeNode reconstructInOrderPreOrder(TreeNode t,	List<Integer> inOrder, ArrayList<Integer> preOrder) {
		if (inOrder.isEmpty() || preOrder.isEmpty()) return null;
		Integer currElement = preOrder.get(0);
		preOrder.remove(currElement);
		Integer index = findIndexInOrder(inOrder, currElement);
		t = new TreeNode(currElement);
		t.left = reconstructInOrderPreOrder(t.left,  inOrder.subList(0, index), preOrderSubList(preOrder, inOrder.subList(0, index)));
		t.right = reconstructInOrderPreOrder(t.right,  inOrder.subList(index+1, inOrder.size()), preOrderSubList(preOrder,inOrder.subList(index+1,inOrder.size())));
		return t;
	}

public ArrayList<Integer> preOrderSubList(List<Integer> preOrder,List<Integer> inOrderSubList) {
		if (preOrder.isEmpty() || inOrderSubList.isEmpty()) {
			return new ArrayList<Integer>();
		}
		/*if (inOrderSubList.size() == 0) {
			return new ArrayList<Integer>();
		}*/
		int min=preOrder.indexOf(inOrderSubList.get(0)),max = 0; 
		
		for (int i = 0; i < inOrderSubList.size(); i++) {
			int indexCurrElement = preOrder.indexOf(inOrderSubList.get(i));
			if (indexCurrElement <= min ) min = indexCurrElement;
			if (indexCurrElement >= max ) max = indexCurrElement;
		}
		System.out.println("min = " + min + "\tmax = " + max);
		return new ArrayList<Integer>(preOrder.subList(min, max+1));
			
	}

- dilip.rangarajan April 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

assuming the tree is balanced tree ?

- Anonymous April 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pick the first elemnet of Pre[] and search for it in In[]. Make it the root of the tree. LEft of the node contains the elements of In[] before the root (repeat the process recuursively with new Pre[] containing the same number of elements as the In[] for left and contain the elements from left to right).....Similarly for right node

- candis April 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pick the first elemnet of Pre[] and search for it in In[]. Make it the root of the tree. LEft of the node contains the elements of In[] before the root (repeat the process recuursively with new Pre[] containing the same number of elements as the In[] for left and contain the elements from left to right).....Similarly for right node

- candis April 16, 2012 | Flag Reply


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