Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

Right most child of the left subtree of a given node is the inorder predecessor.

node *InorderPredecessor(node *ptr)
      {
           if(ptr->left==NULL)
               return NULL;
          node *p=ptr->left;
          while(p->right!=NULL)
                  p=p->right;
          return p;
      }

- khunima March 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope, consider a right-skewed tree with node values - 5(root), 10, 15, 20.

Now Inorder predecessor(10) = 5. Your code will return null

- arsragavan March 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

keep track of previous visited node ......




void pre(struct node *t,struct node **p,struct node *s){

if(t!=NULL){

pre(t->left,p,s);
if(t==s ) {
printf("%d" ,(*p)->data);
}

*p=t;
pre(t->right,p,s);
}

}

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

make a null check .....for *p ...before printing it

- Anonymous March 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Case 1: Node is left most node of BST.
Return NULL
Case 2: Node has left sub tree.
In this case it is Maximum Node in the Left Sub-Tree. i.e., the right most node in the left sub-tree
would be the in-order predecessor.
Case 3: Node has no left sub-tree.
In this case in-order predecessor will be the node where we took the latest right turn.
C++ version:

NODE * find_predecessor(NODE * root, NODE * node)
{
    NODE * temp = root, *parent = NULL;
    if (node->left != NULL){  // Max of the Left Sub-Tree
       temp = node->left;
       while (temp->right != NULL) 
			temp = temp->right;
       return temp;
    }
 
    while ( temp != node ){ // Find the Ancestor where we took latest Right Turn
       if (node->data < temp->data){
         temp = temp->left;  
       }
       else {
         parent = temp;
         temp = temp->right;
       }
    }
    return parent;
}

- R@M3$H.N March 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is this the right approach. I am keeping track of predecessor value using pre variable. Whenever I reach the given node, I return the tracked predecessor value.

public int inorderPredecessor(BST root, int x, int pre) {
        if (root != null) {
            pre = inorderPredecessor(root.left, x, pre);
            if (root.value == x) {
                System.out.println("Predecessor = " + pre);
                return pre;
            }
            pre = inorderPredecessor(root.right, x, root.value);
        }
        return pre;
    }

- arsragavan March 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java solution using DFS inorder traversal but using a Stack to keep track on predecessor, so space complexity could be bad here. Any other simpler, recursion-only solution is welcome :-)

/*
   *          j            PREORDER: j f a d h o m
   *         / \           INORDER:  a d f h j m o
   *        /   \
   *       /     \
   *      f       o
   *     / \     / \
   *    a   h   m
   *   / \
   *      d
   *
   * Returns true if the item is found and its 
   * predecessor(null inclusive) found
   */
  public boolean inorderPredecessor(Node node, T item, Stack<Node> stk) {
    if (node == null)
      return false;

    if (inorderPredecessor(node.left, item, stk))
      return true;

    if (item.compareTo(node.item) == 0) {
      T pred = stk.isEmpty() ? null : stk.pop().item;
      System.out.printf("inorder-pred of %s is %s\n", item, pred);
      return true;
    } else {
      stk.push(node);
    }

    if (inorderPredecessor(node.right, item, stk))
      return true;

    return false;
  }

  public void inorderPredecessor(Node root, T item) {
    if (root == null) {
      System.out.printf("InorderPredecessor: Tree empty");
      return;
    }

    Stack<Node> stk = new Stack<Node>();
    if (!inorderPredecessor(root, item, stk)) {
      System.out.printf("inorder-pred of %s was not found\n", item);
    }
  }

Few test case outputs:

inorder-pred of j is h
inorder-pred of f is d
inorder-pred of o is m
inorder-pred of a is null
inorder-pred of h is f
inorder-pred of m is j
inorder-pred of d is a
inorder-pred of k was not found
inorder-pred of z was not found

- 1over0 March 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming we keep track of parent node:

public class Node<T> {
    private Node<T> left;
    private Node<T> right;
    private Node<T> parent;
    private T item;

    public static Node inOrderPredecessor(Node node) {
        if (node == null) {
            return null;
        }

        if (node.left != null) {
            return rightMostChild(node.left);
        } else {
            Node prev = node;
            Node prevParent = prev.parent;

            while (prevParent != null && prevParent.right != prev) {
                prev = prevParent;
                prevParent = prevParent.parent;
            }
            return prevParent;
        }
    }

    public static Node rightMostChild(Node node) {
        while (node.right != null) {
            node = node.right;
        }
        return node;

    }

}

- kubusgol March 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the left subtree is not null then the inorder predecessor will be the rightmost child of the left node of the given node whose predecssor has to be find and if it is null then the inorder predecessor can be find as follow :-
Travel up using the parent pointer until we see a node which is right child of it’s parent. The parent of such a node is the predecessor.

node * inOrderPredecessor( node *ptr)
{
  
    if( ptr->left != NULL )
    {
         node *p=ptr->left;
         while(p->right!=NULL)
               p=p->right;
        return p;
   }
 
  node *p = ptr->parent;
  while(p != NULL && n == p->left)
  {
      ptr = p;
      p = p->parent;
  }
   return p;
}

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

struct tnode *rightmost(struct tnode *root)
{
       while(root->right!=NULL)
          root = root->right;
       return root;
}

struct tnode *getpre(struct tnode *root,struct tnode *node,struct tnode *prev)
{
       if(root==NULL)
          return NULL;
       if(root==node)
       {
          if(root->left!=NULL)
             return rightmost(root->left);
          return prev;
       }
       struct tnode *temp=getpre(root->left,node,prev);
       if(temp==NULL)
          return getpre(root->right,node,root);
       return temp;
}

- Nitin April 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Inorder(left-root-right) traversal returns elements in increasing order
Reversing the inorder(right-root-left) will return elements in decreasing order. Using this find the predecessor is easy and doesn't require extra variable to store the previous value.

- rpganesshkumar April 17, 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