Interview Question


Country: United States
Interview Type: Phone Interview




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

if largest element has no child then its parent.
if largest element has left child then left child.

- awasthi.manoj January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

inorder predecessor of the largest node

- Anonymous April 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 5 vote

I think we can do it by reverse inorder traversal

/* Function for reverse inorder */
int data;
void revInorder(struct node* root, int kthLargest)
{
     static int count = 0;
     if(root == NULL)
     {
             return;
     }
     revInorder(root->right, kthLargest);
     count++;
     if(count == kthLargest)
     {
         data = root->data;
         return;
     }
     revInorder(root->left, kthLargest);
}

- Nitin Gupta January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. It doesn't compile cause there is no final return instruction.
2. It doesn't work cause it doesn't check the return value from the recursive calls.
3. It scans the whole tree, instead of stopping after finding the node.

fail.

- gen-y-s January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@gen-y-s : Thanks for pointing out mistakes. Edited the function again. However 3rd issue is not addressed yet.

I can think of workaround like exiting when we find kthlargest, but that is not recommended using in the program.

- Nitin Gupta January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

scenarios: the max value has a left node and no right node, is leaf node.
if lead node - second largest element is its parent
if has left node, second largest element is its left child.
Runtime 0(logn)

- tushar January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

There is a generalized version solution which find the k-largest elements in a BST at the entry "Find the k largest elements in a BST" code.google.com/p/elements-of-programming-interviews/wiki/Programs.

It seems that are solutions for another programming interviews book.

- Anonymous January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

the parent of the largest node(right-most node).

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

public static final Node secondLargest(Node root) {
  if(root == null || root.next == null) {
    return null;
  }
  Node next = root.next;
  Node larger = next.next;

  if( larger != null) {
    return secondLargest(next);
  } else {
    return next;
  }
}

- Trent January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static final Node secondLargest(Node root) {
  if(root == null || root.right == null) {
    return null;
  }
  Node next = root.right;
  Node larger = next.right;
  if( larger != null) {
    return secondLargest(next);
  } else {
    return root;
  }
}

- trentenward January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Consider a 2 node tree which has root and left child only.For your solution it will return NULL

- Lalit January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thats true. Thanks. I'll change it soon.

- trentenward January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 4 vote

Idea:
1. Find the right most node. If the right most node has no left child, then its parent is second largest.
2. If the right most node has a left child, then the largest in left part will be the second largest.
public static Node SecondLargest(Node root)
{
   if(root==null)||(root.left==null&&root.right==null)
       return null;
   Node large=root,secLarge;
   while(large.right!=null)
   {
        large=large.right;
        secLarge=large;
    }
    if(large.left==null)
       return secLarge;
   return Largest(large);
}

public static Node Largest(Node root)
{
    while(root.right!=null)
        root=root.right;
    return root;
}

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

The last statement of the first function should be:
return Largest(large.left);

- Simon January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

1. Go to the right most node (will give you the largest node)
2. Get the predecessor of the above
a. If the largest node has a left subtree, get the max of that sub tree
b. Else, the parent of the largest node is the 2nd largest node.

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

You will probably get bonus points if you talk about implementing it in using max, predecessor.

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

go in-order and track the previous and current elements until end, sample of the code in Javascript:

var prev=null, current = null;

tree.inOrderTraversal(function(node){
  if(!prev){
    prev = current =  node;
  }
  else{
    prev = current;
    current = node;
  }
});

console.log(prev.data);

- S.Abakumoff January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Go to the right most node
2.1 if it has left child , then the biggest value from its left child is the result
2.2 if not then the result is its parent.

Important : verify if the tree has at least 2 values

- Kamy January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Store parent aswell during the traversal

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

Why don't we try inorder traversal as it clearly mentioned its a BST. So, it will print elements in ascending order. So, the 2nd element from the right most is the answer.

- prashanthreddy.mtech March 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static Node secondLargest(Node root) {
        if (root == null || root.left == null && root.right == null) return null;
        Node curr=null,prev=null;
        if (root.right == null) {
                 curr= root.left;
            while (curr.right != null)
                curr = curr.right;
            return curr;

        }else{
            curr=root;
          while(curr.right!=null){
              prev=curr;
              curr=curr.right;
          }
              return prev;
        }
    }

- PM April 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Using recursion .

void nthLargest(Node *root,int n)
{
if(root!=NULL)
{
nthLargest(root->right,n);
count++;
if(count==n)
printf("\n nthlarget element is %d",root->data);
}
}

- raju.nitrr May 09, 2015 | 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