Amazon Interview Question for Software Engineer / Developers


Team: payments team
Country: India
Interview Type: In-Person




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

Pseudo-code

public Node construct(int preorder[]) {
    return construct(preorder, 0, -INF, INF);
}
private <Node, int> construct(int preorder[], int i, int min, int max) {
    if (i >= preorder.length) {
        return <null, i>;
    }
    if (!(preorder[i] <= max && preorder[i] > min)) {
        return <null, i>;
    }
    Node node = new Node(preorder[i++]);
    <node.left, i> = construct(preorder, i, min, node.data);
    <node.right, i> = construct(preorder, i, node.data, max);
    return <node, i>;
}

- rix November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

geeksforgeeks.org/archives/25268

- pk November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int index = 0;

public Node createBSTPreOrder(int[] a) {
	return build(a, Integer.MIN_VALUE, Integer.MAX_VALUE);
}

private Node build(int[] a, int min, int max) {
	Node n = null;
	if (index < a.length && a[index] > min && a[index] < max) {
		n = new Node(a[index++]);
		n.left = build(a, min, n.data);
		n.right = build(a, n.data, max);
	}
	return n;
}

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

Looks neat !

- amit November 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is parent pointer allowed in the bst structure??
if so, its quite simple O(n).

if not then do a modified binary search on the array and get the elements
on the right and left of root, similarly proceed with left and right subtree.

comlexity= logn +2log(n/2)+4log(n/4).... = O(n). (its same as for build heap).

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

Put the element in to a stack s. O(n).

Pop every element from the stack and put it in either left or right node. you need to keep track of the root node, currentLeftNode and currentRightNode. O(n) time. This is same technique used by compiler to evaluate any expression where roots are operator.

- Jhasketan November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please elaborate bit more or suggest any links where we can refer?

- Andi November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

With the given pre order there could be many possible BSTs. How do you find a single BST. Multiple BSTs can have same preorder.

If he has given inorder in addition to preorder, We can calculate that.

- Eswar November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am assuming the pre order list contains operands and operators otherwise its not possible to build a unique BST.
This can be done in O(n) time by using stacks.

Algorithm:
Read input preorder expression from the end i.e right to left
Run the below loop for the length of the elements in the pre order expression
If the element is an operand push it to stack.
If the element is an operator pop an element from stack. Set it as the left child. Pop another element from stack.Set it as the right child.
Push the operator(containing the left and right childs) to the stack.
Return the last element as the root of the build BST.

- Mani K November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using stack

public Node buildPre(int[] a) {
	Stack<Node> s = new Stack<Node>();
	root = new Node(a[0]);
	s.push(root);
	
	for (int i = 1; i < a.length; i++) {
		Node n = null;
		while(!s.empty() && a[i] > s.peek().data)
			n = s.pop();
		
		if (n == null) {
			s.peek().left = new Node(a[i]);
			s.push(s.peek().left);
		}
		else {
			n.right = new Node(a[i]);
			s.push(n.right);
		}
	}
	return root;
}

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

First element will be the root and there will be decreasing sequence followed by an increasing sequence. Decreasing sequence will be left subtree and increasing sequence will be right subtree.

Tree(pre[],int start,int end)
     {
             node *ptr=(node *)alloc(sizeof(node));

             ptr->data=pre[start];

             for(int i=start+1;i<end;i++)
                   if(pre[i+1]>pre[i])
                         break;

            ptr->left=Tree(pre,start+1,i)
            ptr->right=Tree(pre,i+1,end)

            retrun ptr;
}

- khunima April 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Another approach could be to build the root node using the 0th element in the input array.
Then, iterate over the remaining elements in the input array and insert into the BST.
Thus, we get worst case O(n) for the operation

- VJ November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

insertion for BST is O(logn) not O(1)

- Anonymous November 04, 2012 | 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