Ebay Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

public class KthLargestBST {

static int count = 0;

public static void kThLast(Tree root, int k) {
if (root != null) {
kThLast(root.right, k);
count++;
if (count == k) {
System.out.println(root.data);
return;
}
kThLast(root.left, k);
}
}
}

- Arjun December 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I traversed it in reverse as we need largest , else its same a inorder traversal just RNL instead of LNR .

- Arjun December 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your algo is O(N).
Small optimization: It would be better to quit the traversal once u find that node.

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

i would suggest minor change to avoid printing multiple elements...

public class KthLargestBST 
{ 

	static int count = 0; 

	public static void kThLast(Tree root, int k) 
	{ 
		if (root != null) 
		{ 
			kThLast(root.right, k); 
			count++; 
			if (count == k) 
			{ 
				System.out.println(root.data); 
				//This will avoid printing rest elements while returning 
				count++; 
				return; 
			} 
			kThLast(root.left, k); 
		} 
	} 
}

- anonymous August 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use inorder traversal and find the (n-k)th element which will be the kth largest element in the tree having n nodes

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

O(logn) is expected.

- bulelaugh December 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@bulelaugh: that's only possible for general balanced BSTs if the tree carries some extra information to enable this operation. See "Order Statistic Tree" on Wikipedia.

- eugene.yarovoi December 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i too don't think that it is possible to do it in O(log n) time

- The Artist December 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

Sahi bola ekdum

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

// where k=k-th largest value and val=value of that data node
void kthLarge(struct Node *node,int &k,int &val)
{
if(!node)
return;
kthLarge(node->left,k,val);
k--;
if(k==0)
val=node->data;
kthLarge(node->right,k,val);
}

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

//Add one more property in the nodes (as no of nodes in subtree rooted at node)
// node.noOfNodes = node.left.noOfNodes+node.right.noOfNodes + 1
//leafNode.noOfNodes = 1
findKthLargest(Node node,int k)
{
if(node != null)
{
if(node.NoOfChild > k )
findKthLargest(node.right, n-k)
else if(node.NoOfChild == k )
return node.data
}
}

- kuldeep.hbti December 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void kthLargest(TREE *ptr,int kth)
{
static int k=0;
if(ptr==NULL)
return ;
else
{
kthLargest(ptr->left,kth);
k=k+1;
if(k==kth)
printf("the kth largest element=%d",ptr->data);
kthLargest(ptr->right,kth);

}
}

- jkl December 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Seq {
        public int num;
        Seq(int n){
            num = n;
        }
    }

public Node find(Node node, Seq seq, int k){        
        if(node == null){
            return null;
        }
        Node n = find(node.left, seq, k);
        if(seq.num == k){ return n;}
        seq.num ++;               
        if(seq.num == k){
            return node;
        }
        return find(node.right, seq, k);
    }

- xietao.mailbox@gmail.com December 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public TreeNode kthLargestNode(int k) {
		return kthLargestNodeT(root, k);
	}

	private TreeNode kthLargestNodeT(TreeNode node, int k) {
		if (node == null) {
			return null;
		}
		int L = sizeOfBSTT(node.left) + 1;
		if (L == k) {
			return node;
		}
		if (L > k) {
			return kthLargestNodeT(node.left, k);
		} else {
			return kthLargestNodeT(node.right, k - L);
		}
	}

	public int sizeOfBST() {
		return sizeOfBSTT(root);
	}

	private int sizeOfBSTT(TreeNode node) {
		if (node == null) {
			return 0;
		}
		return sizeOfBSTT(node.left) + 1 + sizeOfBSTT(node.right);
	}

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

public TreeNode<Integer> kthBST(TreeNode<Integer> root, int k) {
        if(root == null)
            return null;

        if(k < 0)
            return null;

        int left = size(root.left);

        if(k == left + 1)
            return root;
        else if(k < left)
            return kthBST(root.left, k);
        else
            return kthBST(root.right, k - left - 1);
    }

    private int size(TreeNode<Integer> root) {
        if(root == null)
            return 0;

        return size(root.left) + size(root.right) + 1;
    }

- inheritance December 03, 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