Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

Do the nodes have references to their parent nodes? If so, the next node is either the leftmost child of the right subtree, or the first ancestor node of which the node is in the left subtree of if the node does not have a right child. If not, and the node has no right child, just do an in-order traversal of the tree until you find the node, and output the next node.

Assuming you do have a reference to the parent:

public Node findNext(Node s) {
    Node result;
    if (s.rightChild != null) {
        result = s.rightChild;
        while (result.leftChild != null) result = result.leftChild;
        return result;
    } else {
        result = s.parent;
        Node next = s;
        while (result != null && result.rightChild != null && result.rightChild == next) {
            next = result;
            result = result.parent;
        }
        return result;
}

- Anonymous October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and if we do not have a parent node, then:

node *wanted; // the node whose successor we wish to find
node *find_rec(node *t, bool& found) {
    if(t == 0) {
        found = false;
        return 0;
    }
    if(t == wanted) {   // found the 'wanted' node
        found = true;   // in the traversal
        return 0;
    }
    node *x = find_recurs(t->left, found);
    if(found) {
        if(x != 0)
            return x; // the successor already found: just pass it by
        return t; // this is a successor
    }
    return find_rec(t->right, found);
}

node *inorder_succ() {
    if(wanted->right) {
        ... // proceed as in the above soln
        return t;
    }
    bool found = false; // otherwise start the search from the root
    return find_rec(root, found);
}

- pavel.em October 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We do not have access to root! this solution won't work

- Anonymous October 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well, as I said we either have a parent pointer then the solution of anonymous above works, otherwise we must have access to the root, then my solution works

- pavel.em October 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it's better to follow the inorder travel recursive as:
Inorder(root)
{
if (!root) return;
Inorder(root->left);
visit(root);
Inorder(root->right);
}

So rewrite your code as:

node *wanted; // the node whose successor we wish to find
find_rec(node *root, bool& found, node *successor) {
if(root == 0) {
found = false;
return 0;
}
find_recurs(root->left, found, successor);
if(found) {
successor = root; // this is a successor
//reset found to finish the recursive
found = false;
return;
}

if(root->data == wanted) { // found the 'wanted' node
found = true; // in the traversal
}

find_rec(root->right, found, successor);
}

- Anonymous October 19, 2011 | 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