Amazon Interview Question for Software Engineer / Developers


Team: Development
Country: India
Interview Type: In-Person




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

We can keep do a recursive inorder traversal and keep flag to indicate f we have found the value whose successor to be find. Once the value has been found print the next value and return

void findSuccessor(struct node* head, struct node* key, bool & found)
{
if (head == NULL)
return;
findSuccessor(head->left, key, NULL);

if (*found !=False)
{
print( "found it: %d",head->info;
return;
}
if (head==key)
*found = True
findSuccessor(head->right, key, found);
}

- vik May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the inorder traversal for a given node, the successor will be the very next node...
Since all the nodes are arranged in increasing order...

Eg : 1 , 2, 5, 6,7,9,10,15.. (Inorder of a BStree)
Successor of 7 is 9..

This works when right subtree of the node for which successor needs to be found is not null.

- Vijay Rajanna February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you have to do this with minimum traversal, traversing whole tree is not an optimal solution.

- Ajay Kumar February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the right subtree is not null then find the min of right subtree.

Other wise start from root (since the parent pointer is not given (assumption)
find the first node who is the left child of it's parent and return the parent of that node.



if parent pointer is given then

if right[x] ≠ NIL
then return TREE-MINIMUM (right[x])
y ← p[x]
while y ≠ NIL and x = right[y]
do x ← y
y ← p[y]
return y

- NaiveCoder February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the right subtree is not null then find the min of right subtree.

Other wise start from root (since the parent pointer is not given (assumption)
find the first node who is the left child of it's parent and return the parent of that node.



if parent pointer is given then

if right[x] ≠ NIL
then return TREE-MINIMUM (right[x])
y ← p[x]
while y ≠ NIL and x = right[y]
do x ← y
y ← p[y]
return y

- NaiveCoder February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just do the reverse inorder traversal..i.e. right , data(current) , left.. and save the current in a static/global variable.... do recursion.. when current matches the node whoese successor is to be found.. return the value of the static variable...
var=NULL //global variable
successor(current,node)
{
if(node==current)
return var;
successor(current->right,node);
var=current;
successor(current->left,node);
return var;
}

- sumit February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What happens if the node is the root? I think in your code, NULL will be returned

- sai25590 March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

just do the reverse inorder traversal..i.e. right , data(current) , left.. and save the current in a static/global variable.... do recursion.. when current matches the node whoese successor is to be found.. return the value of the static variable...
var=NULL //global variable
successor(current,node)
{
if(node==current)
return var;
successor(current->right,node);
var=current;
successor(current->left,node);
return var;
}

- sumit February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sumit: can you explain how your above code works, I am confused as to what your static variable is and what your current is.

- CJ February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

keep a static flag intiliased to 0 ..wen we find the (whose succesor we r to find out)node ...make that flag =1.and continue traversal.wen flag=1 we return that node as the required node(successor)

- raktimsaha.080 February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void InorderSuccessor(TreeNode root, TreeNode parent)
        {
            if (root.Left != null)
            {
                InorderSuccessor(root.Left, parent);
                TreeNode r = root.left;
                while (r.Successor != null)
                {
                    r = r.Successor;
                }
                r.successor = root;
            }
            else
            {
                if (parent != null)
                    parent.successor = root;

            }

            if (root.right != null)
            {
                InorderSuccessor(root.Right, root);
            }
        }

- Ran February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sometimes, children also be a successor...

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

The succesor of a node in inorder traversal is the parent.

if(node==root)
return node->right->value;
else if(node==root->left || node==root->right)
return root;
else
return(parent(node));

- anni February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry a bit flawed approach..
will work if left n right child exist but will fail in some cases..

- anni February 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

treeNodePtr inorderSucessor(treeNodePtr N) {
        if(N) {
                treeNodePtr tmp;
                // CASE 1: right child of N exists.
                if(N->right) {
                        tmp = N->right;
                        // find leftmost node.
                        while(tmp->left) {
                                tmp = tmp->left;
                        }
                // CASE 2: No right child.
                } else {
                        // keep climbing till you find a parent such that
                        // node is the left decedent of it.
                        while((tmp = N->parent)) {
                                if(tmp->left == N) {
                                        break;
                                }
                                N = tmp;
                        }
                }
                return tmp;
        }
        // No IOS.
        return NULL;
}

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

k //> given node
visitedK = false;
p = root;
do
{
	while( p!=NULL )
		stack.push(p);
		p = p->left;
	
	while( !stack.empty() ) // while - go to but the left node ladder
	{	
		temp = stack.pop()
		visit(temp);
		if( visitedK == true )
			return temp;
		if( temp == k )
			visitedK = true;
		if( temp->right )
			p = temp->right;
			break;
	}
}while( p!= NULL || !stack.empty() );

- y2km11 February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming no parent pointers i.e. each node has just left and right pointers/children.

void findSuccessor(BinaryTree * root, BinaryTree * node, BinaryTree ** successor)
{
	if(root)
	{
		if(root->getLeft() == node)
		{
			*successor = root;
			return;
		}
		findSuccessor(root->getLeft(), node, successor);
		findSuccessor(root->getRight(), node, successor);
	}
}

BinaryTree * inorderSuccessor(BinaryTree * root, BinaryTree * node)
{
	BinaryTree * successor = root;

	if( !node )
	{
		return 0;
	}

	if( node->getRight() )
	{
		successor = node->getRight();
		while(successor->getLeft())
		{
			successor = successor->getLeft();
		}
	}
	else
	{
		findSuccessor(root, node, &successor);
	}

	return successor;
}

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

1. Check if the node is an internal node or external node.
2. if internal then return its right child's inorder traversals first node as succerssor node
3. else if external then check if it is the leftnode or right node ( p = parent(node) ,node =
left or right ? )
4. if leftnode then return parent(node) as successor
5. if rightnode then return parent(parent(node)) as the succesor

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

1. Check if the node is an internal node or external node.
2. if internal then return its right child's inorder traversals first node as succerssor node
3. else if external then check if it is the leftnode or right node ( p = parent(node) ,node =
left or right ? )
4. if leftnode then return parent(node) as successor
5. if rightnode then return parent(parent(node)) as the succesor

- iamgame March 01, 2012 | 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