Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

// Return pointer to the kth largest
Node* kthlargest(Node* root, int &k)
{
	if( root == NULL )
		return NULL;
	else
	{
		Node* result = kthlargest(root->right, int k);
		if( result )
			return result;
		k--;
		if( k == 0 )
			return root;
		return kthlargest(root->left, k);
	}
}

- Sani October 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is the code that prints the kth largest element,

puddleofriddles.blogspot.com/2012/01/find-kth-largest-element-in-binary.html

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

We can do reverse inorder traversal of a BST in order to obtain kth largest element, by keeping global variable count for number of elements seen so far.

Other method would be to use order statistics on BST, i.e. to store count of nodes in left subtree and right subtree in a node.

- sg October 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sg

Can you please explain what do you mean by order statistics?
Can you explain with an example.

- learner October 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

mirror of in-order traversal, keep a count

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

in order traversal where larger child comes first. take an iterative approach so that we can break early

public static V kthLargest(BinaryTree<V> root, int k) {
		Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
		BinaryTree<V> node = root;
		int i = 1;
		V retval = root.key;
		while(!stack.empty() || node != null) {
			if(node != null) {
				stack.push(node);
				node = node.rightChild;
			} else {
				node = stack.pop();
				retval = node.key;
				if(i++ == k) break;
				node = node.leftChild;
			}
		}	
		return retval;
	}

- sola February 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// use morris transversal if we want O(1) space

stack<TreeNode*> s;
bool done = false, cnt = 0;; 
while (!done)
{
    while (root) { s.push(root); root = root->left; }
    if (!s.empty())
    {
          ++cnt;
          if (cnt == k) return s.top();
          root = s.top()->right;  s.pop();
    }
    return NULL;
          
}

;

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

stack<TreeNode*> s;
bool done = false, cnt = 0;; 
while (!done)
{
    while (root) { s.push(root); root = root->left; }
    if (!s.empty())
    {
          ++cnt;
          if (cnt == k) done = true; 
          root = s.top()->right;  s.pop();
    }
    else done = true;
}        
return s.empty() ? NULL : s.top();

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

thLargest(struct bst *start,int k)
{
static count=0;
if(start)
{
kthLargest(start->right,k);
printf("%d->",start->data);
count++;
//printf("count =%d->",count);
if(count==k)
{
printf("\nKthLargest is %d\n",start->data);
return ;
}
kthLargest(start->left,k);
}

- neelabh October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 7 vote

// Find Kth large Element in BST.
// 1. "Rev inorder" Traversal will Give desired result
// similarly we can get kth smallest Element using "Inorder traversal"

int BSTTree :: kthLarge (BSTNode* start, int k)
{
	
	if (start != NULL)
	{	
		kthLarge (start->right,k);
		
		count ++;
		if (count == k)
			return start->data;
		
		kthLarge (start->left,k);
		
	}

}

- Anonymous October 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice and straight answer.

Thanks
Arul

- Arul October 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Did u try out this code? This would work if you want to print the kth number(replacing return start->data with print). But function on the whole would not return the desired result.

- Sani October 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

recursive .. if a element is found it returns back to the calling function which is itself ..unnecessarily even after answer is found.

- anonymous October 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

this wouldn't work is the scope of the count is local right?

- Anonymous December 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Unfortunately, the code would not be compiled.

int BSTTree :: kthLarge (BSTNode* start, int k)
{
	if (start != NULL)
	{	
		kthLarge (start->right,k);
		
		count ++;
		if (count == k)
			return start->data;
		
		kthLarge (start->left,k);
	        // Function must return a value here!!!
	}
        // Function must return a value here!!!
}

- Aleksey.M January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

kth largest can be found by finding (n-k)th element in the normal inorder traversal

- Anonymous October 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how do you know n? Question says to find in single traversal

- msd October 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can count the number of nodes while doing an in-order traversal.. and then use the above logic

- Bhinav January 22, 2013 | 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