Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

private static Node next = null;

        public static void FillInOrderSuccessors(Node root)
        {
            if (root == null) return;
            FillInOrderSuccessors(root.right);
            root.next = next;
            next = root;
            FillInOrderSuccessors(root.left);
        }

- m May 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void GetInOrderSucc(BNode *Root)
{
    BNode *Curr = Root;
    BNode *RNext;

    while(Curr)
    {
        RNext = Curr->Right;
      
        if(!RNext)
        {
            Curr = Curr->Left;
             continue;
        }

        while(!(RNext->Left) && RNext->Left != Curr)
                       RNext = RNext->Left;

        if(!(RNext->Left))
        {
            RNext->Left = Curr;
            Curr = Curr->Right;
        }
        else
        {
             RNext->Left = NULL;
             Curr->Next = RNext;
              Curr = Curr->Left;
        }
     }//while
}

- Lively May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

basicalgos.blogspot.com/2012/04/45-find-predecessor-successor-of-node.html

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

typedef Node* NodePtr;
void FindSuccessor(NodePtr root, int k, NodePtr& successor)
{
if (root == NULL)
{
return;
}

if (root->data <= k)
{
FindSuccessor(root->right, k, successor);
}

if (successor == NULL || root->data < successor->data )
{
successor = root;
}

FindSuccessor(root->left, k, successor);

}

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

Just testing code formatting to preserve white space, since I'm a newbie.

int main ( ) {
        printf ("Hello World\n");
        return 0;
}

- Nitin May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
/* Assuming no pointer to parent node. * If we had a pointer to the parent node, * the code to find successor is much less * messier. Refer Cormen. */ {{{ typedef struct node { int data; struct node *left; struct node *right; struct node *next; } Node; /* Find if the node with the given data * is present in the given BST. Takes * pointer to root as the initial argument. */ Node* FindNode (int data, Node *node) { if (node == NULL) return NULL; if (data == node->data) return node; if (data < node->data) return FindNode (node->left, data); else return FindNode (node->right, data); } /* The routine that finds the successor of * a given node in the BST. Utilizes FindNode() */ Node* FindSuccessor (int data, Node *root) { Node *node = FindNode (data, root); if (node == NULL) return NULL; /* Node not present */ Node *temp, *succ; /* The case where the given node has a right child. * So the successor is the left most node in the right * sub tree. */ if ( (succ = node->right) != NULL) { while (succ->left != NULL) succ = succ->left; return succ; /* This is the successor */ } /* The case where the given node has no right child. * The successor is the lowest ancestor whose left child * is the given node or whose left child also is an ancestor * of the given node. temp = root, parent = NULL; while (temp != NULL && temp != node) { /* Data is in the left sub tree. So we * surely have a successor. temp might * be a possible successor. Save it and descend * further down the tree. if (data < temp->data) { succ = temp; temp = temp->left; } else { /* Data is in the right subtree. Hence, temp * temp is surely not the successor. So descend * without saving temp. */ temp = temp->right; } } return succ; /* If no successor, then anyhow succ * is initialized to NULL, which is returned. */ } /* This is the routine that finally fills in * the next pointers of each node in the BST. * Travserse each node in the tree and call * FindSuccessor() for each. Start with node = root. */ void getInorderSuccessor (Node *node, Node *root) { if (node == NULL) return; node->next = FindSuccessor (node->data, root); /* Proceed recursively in any fashion. Inorder, pre or post. * I'm traversing in pre-order. Doesn't matter which order */ getInorderSuccessor (node->left, root); getInorderSuccessor (node->right, root); } - Nitin May 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reposting with formatted code.
/* Assuming no pointer to parent node.
* If we had a pointer to the parent node,
* the code to find successor is much less
* messier. Refer Cormen.
*/

typedef struct node {
	int data;
	struct node *left;
	struct node *right;
	struct node *next;
} Node;

/* Find if the node with the given data
 * is present in the given BST. Takes
 * pointer to root as the initial argument.
 */
Node* FindNode (int data, Node *node) {
	if (node == NULL) return NULL;

	if (data == node->data) return node;

	if (data < node->data)
		return FindNode (node->left, data);

	else
		return FindNode (node->right, data);
}

/* The routine that finds the successor of
 * a given node in the BST. Utilizes FindNode()
 */
Node* FindSuccessor (int data, Node *root) {
	Node *node = FindNode (data, root);

	if (node == NULL) return NULL; /* Node not present */

	Node *temp, *succ;

	/* The case where the given node has a right child.
	 * So the successor is the left most node in the right
	 * sub tree.
	 */

	if ( (succ = node->right) != NULL) {
		while (succ->left != NULL) succ = succ->left;

		return succ; /* This is the successor */
	}

	/* The case where the given node has no right child.
	 * The successor is the lowest ancestor whose left child
	 * is the given node or whose left child also is an ancestor
	 * of the given node.

	temp = root, parent = NULL;

	while (temp != NULL && temp != node) {
		
		/* Data is in the left sub tree. So we
		 * surely have a successor. temp might
		 * be a possible successor. Save it and descend
		 * further down the tree.
		if (data < temp->data) {
			succ = temp;
			temp = temp->left;
		}

		else {
		/* Data is in the right subtree. Hence, temp
		 * temp is surely not the successor. So descend
		 * without saving temp.
		 */
			temp = temp->right;
		}
	}

	return succ; /* If no successor, then anyhow succ
			     * is initialized to NULL, which is returned.
			     */
}

/* This is the routine that finally fills in
 * the next pointers of each node in the BST.
 * Travserse each node in the tree and call
 * FindSuccessor() for each. Start with node = root.
 */
void getInorderSuccessor (Node *node, Node *root) {
	if (node == NULL) return;

	node->next = FindSuccessor (node->data, root);

	/* Proceed recursively in any fashion. Inorder, pre or post.
	 * I'm traversing in pre-order. Doesn't matter which order
	 */
	getInorderSuccessor (node->left, root);
	getInorderSuccessor (node->right, root);
}

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

Run time complexity will be o(nlogn). Can anybody suggest in o(n)?

- Akash Jain May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hint: Do a reverse inorder traversal.

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

here you go

Node GetInorderSuccessor(Node node,Node nextNode){
  if(node->right)
    node->next=GetInorderSuccessor(node->right,nextNode);
  else
    node->next=nextNode;
  if(node->left)
    return GetInorderSuccessor(node->left,node);
  else
    return node;
}

call as GetInorderSuccessor(Root,null) and ignore the returned node

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

I think the following is much simpler and efficeint implementation
*/
void getInorderSuccessor (Node node) {
if (node.left == NULL && node.right == null) {
node.next = stack.pop(); //stack is a global variable
return;
}
stack.push(node);
getInorderSuccessor (node.left);
getInorderSuccessor (node.right);
node.next = node.right;
}





node->next = FindSuccessor (node->data, root);



/* Proceed recursively in any fashion. Inorder, pre or post.
* I'm traversing in pre-order. Doesn't matter which order
*/
getInorderSuccessor (node->left, root);
getInorderSuccessor (node->right, root);
}

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

Ignore this . check the next post

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

/*
public class TreeNode {
	public TreeNode levelnext,next,left, right;
	public int data;
	TreeNode(int data) {
		this.data = data;
	}
}

*/
      static TreeNode next;
	
	public static void inorderSuccersor(TreeNode root) {
		if (root != null) {
			inorderSuccersor(root.right);
			root.next = next;
			next = root;
			inorderSuccersor(root.left);
		}
	}

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

Just modify the normal inorder function.

void inorder(Node* r,Node* & tmp)
{
	if (r==0) 
		return;
	inorder(r->left,tmp);
	tmp->next=r;
	tmp=r;
	inorder(r->right,tmp);

}

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

Just modify the normal inorder function.

void inorder(Node* r,Node* & tmp)
{
	if (r==0) 
		return;
	inorder(r->left,tmp);
	tmp->next=r;
	tmp=r;
	inorder(r->right,tmp);

}

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

Could you please explain about this, probably logic ?

- Punit Jain May 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Push elements in stack in inorder.
Then pop each element and assign its successor. Complexity O(n);

Stack stack

Inorder (node *root)
{
if (root == NULL)
return;

Inorder (root->left);

stack.push (root);

Inorder (root->right);
}

node *p1=NULL, *p2=NULL;

while (!stack.empty) {
p2 = stack.pop;
p2->next = p1;
p1 = p2;
}

- Punit Jain May 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

just outta curiosity...is this u?

jainpuneet.com/aboutme

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

No.

- Punit Jain May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

To find next Inorder successor is same as find next bigger node than given node.So next bigger node is lies on either right child of given node (if right child is present) or the nearest ancestor who's left side node is situated.

struct node
{  
   int data;
   struct node *left;
   struct node * right;
};
typedef struct node * Tree;

Tree * getNextInorderNode(Tree root)
{
      Tree succ = NULL;
      while(root)
      {
               succ = root;
               root = root -> left;
       }
       return succ;
}

Tree * getInorderSucc(Tree root, const Tree n)
{
    Tree succ = NULL;
    if(n == NULL)
           return NULL;
    if(n->right)
           return getNextInorderNode(n->right);
    while(root)
    {
           if(root->data > n->data)
           {
                    succ = root;
                    root = root -> left;
           }
           else if (root->data < n->data)
                   root = root ->right;
           else
                   break;
    }
    return succ;
}

- Bidhan Pal June 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Traverse the tree using Inorder traversal and find the Successor of each node and set it

// Call Inorder traversal by invoking inorderTraversal(rootNode);

public void inorderTraversal(TreeNode n) {

		if (n == null)
			return;
		inorderTraversal(n.getLeft());
		// Process The Node and get the Successor of the Node x
		TreeNode succesor = findSuccessor(n);
		n.setSuccessor(succesor);
		if (succesor != null)
			System.out.println("Successor of Node n::" + n.getValue() + " is "
					+ n.getSuccessor().getValue());
		else
			System.out.println("Successor of Node n::" + n.getValue()
					+ " is null");

		inorderTraversal(n.getRight());

	}
public TreeNode findSuccessor(TreeNode x) {

		if (x.getRight() != null) {
			return findMinumum(x.getRight());
		}
		TreeNode y = x.getParent();
		while (y != null && x == y.getRight()) {
			x = y;
			y = y.getParent();
		}
		return y;
	}

	public TreeNode findMinumum(TreeNode x) {
		while (x.getLeft() != null) {
			x = x.getLeft();
		}
		return x;

	}

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

public void inorderTraversal(TreeNode n) {

if (n == null)
return;
inorderTraversal(n.getLeft());
// Process The Node and get the Successor of the Node x
TreeNode succesor = findSuccessor(n);
n.setSuccessor(succesor);
inorderTraversal(n.getRight());

}
public TreeNode findSuccessor(TreeNode x) {

if (x.getRight() != null) {
return findMinumum(x.getRight());
}
TreeNode y = x.getParent();
while (y != null && x == y.getRight()) {
x = y;
y = y.getParent();
}
return y;
}

public TreeNode findMinumum(TreeNode x) {
while (x.getLeft() != null) {
x = x.getLeft();
}
return x;

}

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

Node * get_inorder_successor(Node * node, Node * successor = NULL) {

if(node -> right != NULL)
successor = get_inorder_successor(node->right,successor);
node->successor_node = successor;
successor = node;
if(node -> left != NULL)
successor = get_inorder_successor(node->left, successor);
return successor;
}

- Anonymous August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think the following is much simpler and efficeint implementation
*/
void getInorderSuccessor (Node node) {
if (node.left == NULL && node.right == null) {
node.next = stack.pop(); //stack is a global variable
return;
}
stack.push(node);
getInorderSuccessor (node.left);
getInorderSuccessor (node.right);
node.next = node.right;
}

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

wont work...nextnode is not necessarily the right node

- Anonymous May 14, 2012 | Flag


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