Amazon Interview Question for SDE1s


Country: India




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

int kthlargest(node *root,int k)
{
static int count =0;
	if(root == NULL)
	return;
	kthsmallest(root->right,k);
	count++;
		if(k==count)
		return root->data;
	kthlargest(root->left,k);
}

- swati March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
4
of 6 votes

Nice approach..but you code may not run :) Modified code is..

void kthlargest(Node *root,int k)
{
static int count;
	if(root){	
	kthlargest(root->right,k);
	count++;
		if(k==count){ 
			printf("%d",root->data);
		}
	kthlargest(root->left,k);
	}
}

- shashi vidyarthi March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

This will find (n-k)th largest.

- sandeep March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sandeep: Please check..this is reverse of inorder traversal..it means..it will print data in decreasing(eg..5,4,3,2,1) order. Now to find 2nd largest element..it will print 4.

- Shashi Vidyarthi March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You have not initialized count. Even if you initialize it to 0, it will give you the wrong rank!

- alex March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, this is wrong. You never return the actual value in most cases. Though it is close to an answer which might work. But since you have no explanation whatsoever, I chose to downvote this. On the basis of the code alone(it is incorrect and you use static), this is enough to get a bad score on an interview.

The same question was asked a few days back, under Facebook.

- Loler March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In spite of several bad comments and few issues this is really the crux.

Just a right leaning in-order traversal. Instead of additional counter simply do k-- and print when k reduces to zero. See my modification below.

- simpleOne March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Perform reverse in-order traversal of the given BST and return when the k-th element is found.

int count = 0; 
Stack S;
while n != null) {
    S.push(n);
    n = n.right;
}

while (S.size() > 0) {
    n = s.pop();
    count++;
    if (count == k) {
        return n.value;
    }
    if n.left != null) {
        n = n.left;
        while (n!= null) {
             s.push(n);
             n = n.left;
        }
    }
}

System.out.println("ERROR : k > numver of nodes in the tree");

- kislay.nsit March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. Traverse BST in in-order and push elements in stack
2. Pop the elements from stack. At Kth pop, we will get Kth largest element

- Kalyan March 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey Kalyan ... once you have inorder traversal you can find the kth largest element in O(1) as the elements will be in increasing order. So i think without stack jus writing elements into an array while traversing and then w.r.t index required we will know the kth max element in O(1).

- Ashish19121 March 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

traversing BST and then putting element in stack leads to memory complicity to O(2n). It can be done in single go:

void kthlargest(Node *root,int k)
{
static int count;
	if(root){	
	kthlargest(root->right,k);
	count++;
		if(k==count){ 
			printf("%d",root->data);
		}
	kthlargest(root->left,k);
	}
}

- shashi March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Shashi, it makes sense but you should not use static in such contexts. I think we should rather use a helper function which preserves the value of 'k' and passes a copy of k as reference to your function.

- SecondAttempt March 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void getKthLargestElement(Node *root, int k){
	int num = 0;
	count(root, &num);
	getKthSmallestElement(root,num-k+1,num);
}
//To find kth smallest element in in BST
void getKthSmallestElement(Node *root, int k, int num){
   static int count, found;
   if(root && !found ){	
         getKthSmallestElement(root->left, k, num);
         count++;
         if(count == k){
	        printf("%d",root->data); found =1; return; 
         }
         else if(count == num && !found){
	        printf("\nData not found"); return;
         }
         getKthSmallestElement(root->right, k, num);
   }
}
void count(Node *root, int *p){	
	if(root){
		count(root->left, &(*p));
		(*p)++;
		count(root->right, &(*p));
	}
}

- Razz March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No recursion, beat that.
And traverses as minimum nodes as required.

private static int getKthLargest(TreeNode root , int k) {
		int fixedIndex=0;
		List<TreeNode> list = new ArrayList<TreeNode>();
		fillRightSideNodes(list,root,fixedIndex);
		while(fixedIndex<=list.size()){
			if ( fixedIndex>=list.size()){return -1;}
			if ( fixedIndex==k-1){return list.get(fixedIndex).getValue();}
			fillRightSideNodes(list,list.get(fixedIndex).getLeftSubTree(), fixedIndex+1);
			fixedIndex++;
		}
		return -1;
	}
	private static void fillRightSideNodes(List<TreeNode> list, TreeNode root,
			int index) {
		while ( root != null ){
			list.add(index,root);
			root=root.getRightSubTree();
		}
	}

- gautam142857 March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You seem to be attempting to simulate an iterative in-order (in reverse) traversal using a list...

I guess one can beat that, by actually using a stack instead, where you get to pop off/reuse the stack data, thereby restricting the space usage to O(h) (height of tree), while your space usage is potentially Omega(n) (number of nodes in tree) (you only append to your 'stack').

Also, inspite of it being pseudo code, it looks quite ugly. So writing cleaner code which is easier to understand will better that, I suppose. (Perhaps not me, as I find most of my code ugly too)

- Loler March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your comment needs correction in each line :
1. Its not even a complete traversal of the tree. As I've already written,
I traverse only as few nodes as needed. ( Only the right most ones )
2. I'm using the list as a stack itself. ( The words pop/ push need not always be there to tell you that its a stack )
3. The maximum size the list will grow is not equal to number of nodes in the tree, if you got #1.
4. Its not pseudo code. Its a complete running java code. The Node structure is not pasted as they are too trivial.

- gautam142857 March 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Loler is right.

k=n and you traverse the whole tree, that invalidates your first two comments.

You never remove from list, so you are not actually popping. This invalidates your 3.

This is crap code. This invalidates your 4.

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

public static void findKthElement(Tree node , int k)
{
if(node == null)
{
System.out.println("TREE NULL");
return;
}
Stack<Tree> stack = new Stack<>();
int n =0 ;
while(true)
{
while(node != null)
{
stack.add(node);
node = node.right;
}
if(stack.isEmpty())
{
System.out.println("ELEMENT COUNT OUT OF RANGE");
return;
}
node = stack.pop();
n++;
if(n == k)
{
System.out.println("THE KTh ELEMENT: "+ node.getValue());
return;
}
node = node.left;
}
}

- Mr.karthik.p March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

TreeNode kthLargest(TreeNode t, int k) {
    if (t == null){
        return null;
    }
    return kthLargest(t.right, k);
    k--;
    if (k == 0){
        retrun t;
    }
    return kthLargest(t.left, k);
}

- Frank March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I just realized there is a slight typo.

return kthLargest(t.right, k);

should just be

kthLargest(t.right, k);

- Frank March 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct Node {
int value;
Node *left;
Node *right;
};

//return: true find, false not find
//param:
// count: the number of nodes in the subtree
// ans: the kth largest number
bool find_kth_large(Node *root, int k, int &count, int &ans)
{
if (NULL == root) {
count = 0;
return false;
}
int right_count = 0;
if (find_kth_large(root->right, k, right_count, ans)) {
return true;
}
if (right_count + 1 == k) {
ans = root->value;
return true;
}
int left_count = 0;
if (find_kth_large(root->left, k - 1 - right_count, left_count, ans)) {
return true;
}
count = left_count + right_count + 1;
return false;
}

- brent March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct tree *kthlargBST(struct tree*root,int k)
{
if(k!=0 && root==NULL)
return;
kthlargBST(root->right,k--)
if(k==0)
return root;
kthlargBST(riit->left,k--);


}

- M_I-1 March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

augmenting the bst can also work. The bst should be augmented with the total no of children in its tree

- alex March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, that is the key point.

- Dumbo March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def find_largest_element(node, k)
  return nil unless node
  index = 0
  find_largest_element(node.right, k)
  if ++index == k
    return node.data
  end
  find_largest_element(node.right, k)
end

- ruby March 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class KthLargestBST{
	protected static int findKthLargest(BSTNode root,int k){
		int [] result=findKthLargest(root,k,0);
		return result[1];//the element we are looking for.
	}
	private static int[] findKthLargest(BSTNode root,int k,int count){//returns an array containing 2 ints..one for counts and the other for result.
		if(root==null){
			int[]  i=new int[2];
			i[0]=-1;
			i[1]=-1;
			return i;
		}else{
			int rval[]=new int[2];
			int temp[]=new int[2];
			rval=findKthLargest(root.rightChild,k,count);
			if(rval[0]!=-1){
				count=rval[0];
			}
			count++;
			if(count==k){
				rval[1]=root.data;
			}
			temp=findKthLargest(root.leftChild,k,(count));
			if(temp[0]!=-1){
				count=temp[0];
			}
			if(temp[1]!=-1){
				rval[1]=temp[1];
			}
			rval[0]=count;
			return rval;
		}
	}
	public static void main(String args[]){
		BinarySearchTree bst=new BinarySearchTree();
		bst.insert(6);
		bst.insert(8);
		bst.insert(7);
		bst.insert(4);
		bst.insert(3);
		bst.insert(4);
		bst.insert(1);
		bst.insert(12);
		bst.insert(18);
		bst.insert(15);
		bst.insert(16);
		bst.inOrderTraversal();
		System.out.println();
		System.out.println(findKthLargest(bst.root,7));
	}

}

- laxman601 March 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Some people already did this using C and a static counter. This is using Java and no counter.

This is just a right leaning in-order traversal.
When the counter becomes zero we are done.

void printKthLargest(Node n, int k) {
	if( n==null)
		return ;//reached root's parent

	printKthLargest(n.right, k);
	if( k == 0) {
		System.out.println(k);
		return;
	}
	k--;
	printKthLargest(n.left, k);	

}

- simpleOne March 19, 2013 | 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