Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Revision and simplified version:

public class CartesianTree {
	
	public static class Node {
		public int value;
		public Node left;
		public Node right;
	}
	
	public static Node build(int[] data) {
		if (data == null || data.length == 0) return null;
		return build(data, 0, data.length - 1);
	}
	
	private static Node build(int[] data, int start, int end) {
		if (end < start) return null;
		int min = Integer.MAX_VALUE;
		int minIndex = -1;
		
		for (int i = start; i <= end; i++) {
			if (data[i] < min) {
				min = data[i];
				minIndex = i;
			}
		}
		
		Node node = new Node();
		node.value = min;
		
		node.left = build(data, start, minIndex - 1);
		node.right = build(data, minIndex + 1, end);
		
		return node;
	} 
}

- Anonymous February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Both of you are right; Pseudo Code of constructing cartesian tree from sequence of nodes:
1.Find the lowest number in the sequence suppose A[1..N] is the sequnce of numbers and A[i] is the lowest.
2. Make this A[i] root of the tree.
3. Devide whole tree into two part A[1..i-1] and A[i+1..N]
4. Apply step 1 to 3 on these two subtrees.

- ahmads March 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CartesianTree {
	
	public static class Node {
		public int value;
		public Node left;
		public Node right;
	}
	
	public static Node build(int[] data) {
		if (data == null || data.length == 0) return null;
		return build(data, 0, data.length - 1, null, false);
	}
	
	private static Node build(int[] data, int start, int end, Node parent, boolean fromLeft) {
		if (end < start) return null;
		int min = Integer.MAX_VALUE;
		int minIndex = -1;
		
		for (int i = start; i <= end; i++) {
			if (data[i] < min) {
				min = data[i];
				minIndex = i;
			}
		}
		
		Node node = new Node();
		node.value = min;
		
		if (parent != null) {
			if (fromLeft) parent.left = node;
			else parent.right = node;
		}
		
		node.left = build(data, start, minIndex - 1, node, true);
		node.right = build(data, minIndex + 1, end, node, false);
		
		return node;
	} 
}


}

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

There is a O(n) solution to this problem. Yours is not O(n)

- guest July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public TreeNode build(int[] a)
        {
                TreeNode root = null;
                Stack<TreeNode> stack = new Stack<TreeNode>();
                for(int i = 0; i < a.length; i++) {
                        TreeNode last = null;
                        while(!stack.empty() && stack.peek().val > a[i]) last = stack.pop();
                        TreeNode node = new TreeNode(a[i]);
                        node.left = last;
                        if(stack.empty()) root = node;
                        else stack.peek().right = node;
                        stack.push(node);
                }
                return root;
        }

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

This code has a problem.
By definition root is the minimum of the array,
but the codes seems get the first element as the root of array...?

- Anonymous April 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// following is the O(N) time algorithm for constructing Cartesian tree from in-order traversal of the binary search tree (i.e. sorted sequence).

Please suggest improvement

static Node cTree(int[] a, int start, int end) {
		
		// let's construct 1...
		// for the remaining array...
		// construct the left child... 2n + 1
		// construct the right child...2n + 2.
		if (start > end)
			return null;
		
		int key = a[start];
		Node n = new Node(key);
		n.left = cTree(a, 2*start + 1, end);
		n.right = cTree(a, 2*start + 2, end);
		
		return n;
	}

- Laiq Ahmed June 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is never said that it is a BST....

- rajofchennai March 17, 2014 | 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