Amazon Interview Question for SDE1s


Country: India
Interview Type: Written Test




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

1. If the node has right child. then return leftmost child of the right subtree.
2. Else if node is the left child of its parent then return its parent
3. Else go up to the parent until you find a node that is a left child of its parent. Return the parent.
4. Return null

Here is the code

public TreeElement findNextLargest(TreeElement n) {
        if (n == null) {
            return null;
        }

        TreeElement parent = n.parent;
        TreeElement temp;
        if (n.right != null) {
            temp = n.right;
            while (temp.left != null) {
                temp = temp.left;
            }
            return temp;
        } else if (parent != null && n == parent.left) {
            return parent;
        } else {
            temp = n;
            while (parent != null) {
                if (temp == parent.left) {
                    return parent;
                }

                temp = parent;
                parent = parent.parent;
            }
        }
        return null;
    }

- CodeNameEagle May 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

That's simply the inorder successor..either the leftmost child of the right child or right child or parent (in preference order, depending on existence).

- alex May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Well of course it's the successor of in-order traversal - but it's not that simple to perform in-order traversal iteratively from an arbitrary node.

Your answer is wrong because the given node could be the right child of its parent, so the parent is not the next largest node (it would be a higher ancestor in this case).

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

def nextLarget(node):
  if node.right!=None:
    node=node.right
    while node.left!=None:
      node=node.left
  else:
    while(True):
      prev=node
      node=node.parent
      if node==None or node.left==prev:
        break
  return node

- gen-y-s May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why do we need a parent pointer just wondering. The next largest node will be the left most node of the right node of the given node.

- DashDash May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is why you need parent pointer

minRightChild = getMin(node -> right); //if node->right is null then return intmin
If node->data <= parent->data
	return Max(minRightChild, parent->data)
else
	return minRightChild

- Anonymous May 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the node doesn't have a right child you have to look up.

- bunnybare May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in case if a node has no childrens then its parent could be the next largest. One cannot wonder in the written test for a problem statement

- chetan12189 May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh yes, I just slipped out on that case. well you have your full solution
1) If there are no right child of a given node its parent is the next highest node
2) Else the left most node of the right node of the given node

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

another 2 cases
1.if parent is same as that of given node
2.if given node has no right child and given node is right of parent then...

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

The function 'findNextLargest' will do the job

node* findElement(node* r,int val)
{
	if(!r)
		return NULL;

	if(r->data == val)
		return r;
	if(r->data < val)
		return findElement(r->right,val);
	else
		return findElement(r->left,val);
}
node* findMin(node* s)
{
	while(s->left)
		s = s->left;
	return s;
}
int findNextLargest(node* r,int val)
{
	if(!r || !r->right)
	{
		cout<<"No such element"<<endl;
		return ERROR_CONST;
	}
	node* s = findElement(r,val);
	if(s->right != NULL)
	{
		node* t = findMin(s->right);
		return t->data;
	}
	while(s != r)
		s = s->parent;

	if(s==r)
		return ERROR_CONST;
	
	return s->data;

}

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

Complexity:
Time: best case is O(1), worst case when searching left most of parent is O(n)
Space: O(1)

- subhajit May 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findLargest(node*root , int val) {
if (!root) //tree is empty
    printf("\ntree is empty") ; 
    exit(); 
if (val == root->value) {
    printf("\nOnly 1 element is present");
    exit();
}
node * element ;
if (val < root->value) 
    element = findElement(root->left , value);
else 
    element = findElement(root->right , value);

if (element->right == NULL)  {
    if (element = element->parent->left) { // the element is a left child . So its parent is the                         //next largest 
        return parent;
    }
    //Now the element is a right child . 
     if(element->value > root->value) { //the right most child of the right subtree from the //root node is the largest element
         printf("\nThis element is the largest in the tree");
         exit();
     } 
         
     while()  { // Now have to traverse back through the parent of  
    //each node till we reach the parent of the subtree which is a left child. Now the parent      //of this node will be the next largest.
          if (element = node->parent->left) {
              return parent->value;
          }
          element = element -> parent;
    }
  
}           

//Now the right child is not empty . So the next higher element will be the left-most child of the right-child .
element = element->right;
while(element->left != NULL) {
    element = element->left;
}
return element->value;
}   

findElement(node * root , int val) // This will do a tree traversal to find the right element.

- Sumit May 20, 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