Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

we just need to modify the inorder traversal.. thats it
call this function from main with calling as inordermodify(root);

void inordermodify(struct node *head)
{
static node *temp= NULL;
if(head != NULL)
{
inordermodify(head->right);
head->inordersucc = temp;
temp = head;
inordermodify(head->left);
}
}

- rockstar November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Inorder is left -root-right.
So first call inorder(root->left)

- swathi November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Rockstar - nice! Swathi, the code is correct and purposely from right to left.

Here is a solution without a static variable:

Node * InOrderModify(Node * pNode, Node * pPrev)
{
    if (pNode == NULL)
        return pPrev;
    pPrev = InOrderModify(pNode->pLeft, pPrev);
    if (pPrev != NULL)
        pPrev->pSucc = pNode;
    return InOrderModify(pNode->pRight, pNode);
}

- czpete425 November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@rockstar: +1 Very nice rockstar! clean and simple approach!

- mag November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@czpete425: Thanks a lot to you too! Your solution without static variable is just awesome! You should have entered that as an answer so I could upvote it!

- mag November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@czpete425.. thnx buddy,its a vry gud approach.. gud going..
@mag thanku dear friend....
@swathi.. swathi,work it out on a tree.. u will get d difference,nd yar as i said inorder is been modified

- rockstar November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@rockstar: awesome approach dude!! :)

- ashu1.220791 November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

cool man....

- sruthi November 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@czpete425 I have a question
pPrev = InOrderModify(pNode->pLeft, pPrev)
pPrev is Null till you reach a node with no left child...
should it not be
pPrev = InOrderModify(pNode->pLeft, pNode)

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

@czpete425 can you please explain how do you set syccessor in followng case

4
                   1         6

we call ur fnct with InOrderModify(4,NULL)
it again calls InOrderModify(1,NULL)
then InOrderModify(NULL NULL) and return NULL)
prev=NULL
then InOrderModify(6,4) again return 4
4->setSucc(6) which is again wrong

Please check my suggestion..and point out mistakes(I am sure there are, I am beginner)

Node * InOrderModify(Node * pNode, Node * pPrev)
{
    if (pNode == NULL)
        return pPrev;
    pPrev = InOrderModify(pNode->pLeft, pNode);
    if (pPrev != NULL)
        pPrev->pSucc = pNode;
    return InOrderModify(pPrev->pRight, pNode);//prb here
}

- shallysahi December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous: pPrev is the in-order predecessor not the parent. And you are absolutely correct - if you start at the root and keep going to left childrent only, the current node is the smallest thing you have seen so far, hence no in-order predecessor. Once you backtrack or go to right child, you're no longer the smallest, hence pPrev is not NULL.

@shallysahi: Try stepping through the code one line at a time and write down all the variables. At the same time remember where you are for each recursion. Something like this:

InOrderModify(4, NULL)  // Initial call
    // pNode = 4, pPrev = NULL
    InOrderModify(1, NULL)  // From line 5
        // pNode = 1, pPrev = NULL
        InOrderModify(NULL, NULL) // From line 5
        // pNode = NULL, pPrev = NULL
        return NULL
        InOrderModify(NULL, 1)   // From line 8
        // pNode = NULL, pPrev = 1
        return 1
    return 1
    // pNode = 4, pPrev = 1
    set Succ(1) = 4   // Line 7
    InOrderModify(6, 4)  // Line 8
        // pNode = 6, pPrev = 4
        InOrderModify(NULL, 4)   // Line 5
        return 4
        // Still pNode = 6, pPrev = 4
        Succ(4) = 6  // Line 7
        InOrderModify(NULL, 6)
        // pNode = NULL, pPrev = 6
        return 6
    return 6;
return 6;

Hopefully this will make things more clear ... Also, you can try running the code in a debugger ...

- czpete425 December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you for explaining.I appreciate your concern.tons of Thanks

- shallysahi December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@czpete425 I have one question where pPrev pointer is modified. If i call it InOrderModify(root,NULL);

- MI November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@MI: InOrderModify(pRoot, NULL) is exactly how you should call this function. If you do, and assuming that pRoot != NULL, you keep calling

pPrev = InOrderModify(pNode->pLeft, pPrev);

recursively until you find a node with no left child. Then pPrev becomes that node because you call

pPrev = InOrderModify(pNode->pLeft, pNode);

- czpete425 November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

exactly the same to my solution!

- Anonymous November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Void InorderSuccessor(node* root, node* pre)
{
if(!root)
return;
InorderSuccessor(root->left,pre)
if(pre)
pre->succ = root;
pre = root;
InorderSuccessor(root->right,pre);
}

- MI November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@MI: Your solution is correct too!
@ downvoter: care to explain?

- mag November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I did not down-vote but there's a mistake ... The function signature needs to be

Void InorderSuccessor(node* root, node* & pre);

In which case the algorithm is identical to what I posted above only instead of returning a pointer, you set it in the in-out parameter.

- czpete425 November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

initail call, succ will be null

Successor(Head,Succ)
{
if(head->right == null)
head->succesor = succ
else
head->succ= successor(head->right,succ)
if(head->left == null)
return head
else
return succesor(head->left,head)
}

- kannan s December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yep rockstar nailed it !

- jay November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@czpete425 awesome solution .. +1

- prashant November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node * inorder(node * root,node * prev)
{
    if(root==NULL) return prev;
    node *temp=inorder(root->left,root);
    root->succ=inorder(root->right,prev);
    return temp;
}

- kpshah123456 December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I had a hard time parsing the algorithm presented at the top of the page, that uses no temp variable. Eventually, I went back to the basics of recursion and figured out how the algorithm works. Thought I'll present my analysis here in case it can be of use.

Let us assume we are at a Node n. There are two parts to this solution: 1) make n as the successor of a node in the left child tree 2) make a node in the right child tree a successor of n.

Lets write the algorithm for the 1st case. Assume a recursive call into the left child tree returns a node whose successor is n. We then set n as the successor of this node, then we make a recursive call into the right child tree. The node that is returned from this call, we pass up to the level above. For the base case, when we reach the leaf node, we simply return this node. Lets write code for this:

public Node inOrderSuccessor(Node n) {
    if (n.left == null && n.right == null) {
        return n;
    }

    Node prev = inOrderSuccessor(n.left);
    prev.successor = n;
    return inOrderSuccessor(n.right);
}

Now, on to the 2nd case. The successor of Node n is the first node in the right child tree with no left child. We must pass n into the recursive call and set its successor when the above condition is reached.

public  Node isOrderSuccessor(Node n, Node firstAncestorWithRightChild) {
    if (n.left == null) [
        firstAncestorWithRightChild.successor = n;
    }

    if (n.left == null && n.right == null) {
        return n;
    }

    Node prev = inOrderSuccessor(n.left, firstAncestorWithRightChild);
    prev.successor = n;
    return inOrderSuccessor(n.right, n);
}

Hope this is useful.

- Anonymous March 10, 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