Amazon Interview Question for Software Engineer / Developers






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

Maintain a vector of nodes seen so far as we recursively visit the tree. In C++:

struct Node
{
    int value;
    Node* left;
    Node* right;
};


void visit(Node* n, std::vector<int>& v)
{
    if (n)
    {
        for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i)
        {
             matrix[*i][n->value] = 1;
        }
        v.push_back(n->value);
        visit(n->left, v);
        visit(n->right, v);
        v.pop_back();
    }
}

- cristi.vlasceanu July 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

summing up each post to answer :) just intelligent pre-order traverse insert in the array 1 for the node you travel (parent will be available) so you get the array with 1 incase of parents ...

find transitive closure of the matrix thats the answer

- Anonymous August 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nice Algo!!!

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

will someone explain the code a bit...will be better for others to get the hang of whats going on..
thank you.

- MrSmartyPants July 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

[1] Do a pre-order traversal and make an array
[2] for this array (assuming the index starts from 1) for each element at position k the elements at 2k and 2k+1 are its children. hence mark a[k][2k] = 1 and a[k][2k+1] = 1

- Appy July 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Only if it is a Complete Binary Tree 2K and 2K+1 will work. Or else you have to fill the array with blank spaces for missing nodes to make it a complete one.

- kuberan marimuthu July 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

not only a[k][2k] and a[k][2k+1] shud be marked as 1. The question says "ancestors" and not just "parents".
so a[k][2k],a[k][2k+1],a[k][4k],a[k][4k+1].. shud be marked 1.

- ssn July 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question doesn't mention that it has to be a binary tree.

- Erik July 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can call the method for each child.

- Anonymous July 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

simply do dfs and keep the ancestor element in the stack ......and fill the matrix with the stack element....

- Annnn August 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think pre order makes too many assumptions ... I would go for the DFS method

- abhimanipal April 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dunno why ppl dont read the question carefully ... it does not say a binary tree. I suppose we shall write code for an Nary tree.

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

Can u give an example?

- Rad November 09, 2010 | 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