Amazon Interview Question for Software Engineer / Developers






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

time complexity o(n2) space o(n)

public static void main(String[] args) {
Tree root = newNode(10);
Tree n1 = newNode(15);
Tree n2 = newNode(22);
Tree n3 = newNode(7);
Tree n4 = newNode(18);
Tree n5 = newNode(17);
Tree n6 = newNode(77);
root.left = n1;
root.right = n4;
n1.left = n2;
n1.right = n3;
n4.left = n5;
n4.right = n6;


List<Tree> l = levelOrderTrav(root);
System.out.println("Size : " + l.size());
System.out.println("largest size BST in given BT: " + getLargestBSTInBT(l));

}

private static List<Tree> levelOrderTrav(Tree root){
List<Tree> l = new ArrayList<Tree>();
if(root == null){
return l;
}
Queue<Tree> q = new LinkedList<Tree>();
q.add(root);
while(!q.isEmpty()){
Tree n = q.poll();
l.add(n);
if(n.left!=null){
q.add(n.left);
}
if(n.right!=null){
q.add(n.right);
}
}

return l;
}


private static int getLargestBSTInBT(List<Tree> l){
int len =0;
int i=0;
int size = l.size();
int tempLen =0;
for(i=0;i<size;i++){
Queue<Integer> q = new LinkedList<Integer>();
q.add(i);
tempLen =1;
while(!q.isEmpty()){
int j = q.poll();
if(2*j+1 < size){
if(l.get(2*j+1).data<l.get(j).data){
tempLen++;
q.add(2*j+1);
}
}if(2*j+2 < size){

if(l.get(2*j+2).data>= l.get(j).data){
tempLen++;
q.add(2*j+2);
}
}
}
if(tempLen>len){
len = tempLen;
}
}
return len;
}

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

Any traversal or something like that doesnot work. Use dynamic programming to solve it

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

Not sure if DP helps.

Trees rooted at root->left and root->right being BSTs doesn't say anything about the tree rooted at 'root'!! We should calculate this all over again.

- Mahesh September 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void maxBSTSize(BiTree t){
            Pair p = maxBSTsizeHelper(t, 0);

            System.out.println(p.max);
        }


        public static Pair maxBSTsizeHelper(BiTree t, int max){
            Pair p = new Pair();

            if(t==null){
                p.max = max;
                p.current = 0;
                return p;
            }

            else{
                Pair left;
                Pair right;

                p.current = 1;
                left = maxBSTsizeHelper(t.left, max);
                right = maxBSTsizeHelper(t.right, max);

                if(t.left!=null && t.Node >= t.left.Node){
                    p.current = p.current + left.current;


                }



                if(t.right!=null && t.Node < t.right.Node){
                    p.current = p.current + right.current;
                }

                p.max = Math.max(left.max, right.max);
                p.max = Math.max(p.max, p.current);


            }


            return p;



        }

public static void main(String args[]){
BiTree t = new BiTree(10, new BiTree(15, new BiTree(22, null, null), new BiTree(7, null, null)), new BiTree(18, new BiTree(17, null, null), new BiTree(77, null, null)));
maxBSTSize(t);
}

- hi guys September 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Basicly the idea is the following:
At each node, we produce the size of the BST with root as current node.
For this:
1) the current node contributes to the size as 1.
2) we check if the left branch contributes and add the size from there (recursive step)
3) we check if the right branch contributes and add the size from there (recursive step)
4) we then compare the size of the BST rooted at the current node with the max. sized BSTs of the left and right branch.
5) we return two items:
a) the size of the BST with current node as root
b) the size of the largest BST found as a subtree of the tree with current node as root

One correction (altough the code does work without this correction): the "max" parameter to recursive call is unnecessary and the base case (null node) should be the following:

if(t==null){
                p.max = 0;
                p.current = 0;
                return p;
            }

- hi guys September 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the input is a binary tree (need not to be a bst).
it need to satisfy two conditions:
left <= parent <right and min < left,right<max. the min and max values will come from the ancestors. for root min and max are -infinity and +infinity. then for left tree -->min is -infinity,max is root.value. and for right tree --> min is root.value,max is +infinity.

Now we can compute the max BSt by returning the nodes in tree which are BST. the magnitude (+/-) will indicate the bst relationship status.

- Hinax September 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Forgot to define Pair:

class Pair {
    public int max;
    public int current;

    Pair(){
        this.max=0;
        this.current = 0;
    }
}

- hi guys September 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Steps:
1. Create inorder array for the entire tree.
2. Now Find the longest sorted contiguous sub-sequence in the Array

1. Keep an auxiliary array of the size of the original array and initialize it with zeros. The i’th element of this array holds the number of elements which are in sorted order after index i in the original Array.

2. Traverse the original Array from right to left
if(original[i] < original[i+1])
auxiliary[i] = auxiliary[i+1] + 1;

3. Find the maximum element in the Auxiliary array and print the elements ahead of that index from the original array.

- Ratnesh September 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

consider : 3(5(4)(6)))(8(9)(10)).
inorder traversal will give:
34569810. taking longest subseq will give:34569 which is not a bst(as 8(9)(10) is not a bst).

- Hinax September 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"consider : 3(5(4)(6)))(8(9)(10)).
inorder traversal will give:"
Isn't the inorder of the given tree must be 4 5 6 3 9 8 10??
Is 3 the root and its children 5, 8...

If that IS the case, then the 4 5 6 is the max BST with size 3. Please correct me if I'm wrong.

- javJav October 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findbst(node *tree)
{
if(tree==NULL)
return 0;

if(isBST(tree))
return size(tree));

if((tree->left->data < tree->data) && (tree->data < tree->right->data))
return (1+findbst(tree->left)+findbst(tree->right))
else if((tree->left->data < tree->data) && (tree->right->data < tree->data))
return (max(1+findbst(tree->left),findbst(tree->right))
else if((tree->left->data > tree->data) && (tree->right->data > tree->data))
return (max(findbst(tree->left),1+findbst(tree->right))
else
return (max(findbst(tree->left),findbst(tree->right))

}

Correct me if it is wrong!!!!

- Elamaran V September 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Inorder traversal of the tree, put the values in an array. Now find the longest increasing subsequence in array

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

See the example given by Hinax responding to Ratnesh. This doesn't work.

- Mahesh September 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just calculating all possible BST sizes Any better solution?

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define MAX_INT 32700
#define MIN_INT -32700

struct treeNode
{
 int data;
 bool isRootOfBST;
 struct treeNode* left;
 struct treeNode* right;
};

//Utility Function
treeNode* createNode(int data)
{
	treeNode* newNode = (treeNode*)malloc(sizeof(treeNode));
	newNode->data = data;
	newNode->isRootOfBST = false;
	newNode->left = NULL;
	newNode->right = NULL;
	
	return newNode;
}

//Utility Function
void printInOrder(treeNode* root)
{
	if(!root) return;
	
	printInOrder(root->left);
	printf("%d ", root->data);
	printInOrder(root->right);
}

bool isBST(treeNode* root, int min, int max)
{
	if(!root) return true;
	
	bool bst = false;
	if(min <= root->data && root->data < max) bst = true;
	return isBST(root->left, min, root->data) && isBST(root->right, root->data, max) && bst;
}

//Returns the number of nodes in the maximum BST
int maxBST(treeNode* root)
{	
	if(!root) return 0;
	
	int leftMax = 0;
	int rightMax = 0;
	
	if(root->left) leftMax = maxBST(root->left);
	if(root->right)rightMax = maxBST(root->right);
	
	if(isBST(root, MIN_INT, MAX_INT)) return 1+leftMax+rightMax;
	else return (leftMax<rightMax)?rightMax:leftMax;
}

int main()
{
	treeNode* root = createNode(18);
	root->left = createNode(47);
	root->right = createNode(10);
	
	root->left->left = createNode(54);
	root->left->right = createNode(30);
	
	root->left->right->left = createNode(28);
	root->left->right->left->left = createNode(7);
	root->left->right->left->right= createNode(29);
	
	root->right->left = createNode(11);
	root->right->right = createNode(13);
	root->right->left->left = createNode(4);
	
	printInOrder(root); printf("\n");
	printf("%d ", maxBST(root));
}

- Mahesh September 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Flatten the binary tree using inorder traversal. Find the largest increasing sub-sequence.

- Anonymous September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Flatten the binary tree using inorder traversal. Find the largest increasing sub-sequence.

- Anonymous September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Flatten the binary tree using inorder traversal. Find the largest increasing sub-sequence.

- Anonymous September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

post order traversal. O(n)

- Anonymous October 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c" line="1" title="CodeMonkey6839" class="run-this">xxxx ()
{
qqqq
}

</pre>

- Anonymous January 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ladka heera hai !!!

- Anonymous January 29, 2011 | 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