Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

you can do smth like this:
that is first reverse all children subtrees recursively, then
change the current node

add_leaf_to_list(node *t) {
// make a linked list out of nodes 't'
}

reverse_tree(node *t) {

    if(t == 0)
        return;

    for(i = 0; i < t->nchildren; i++) {
        reverse_tree(t->child[i]);
        t->child[i]->child[0] = t;
        t->child[i]->nchildren = 1;
    }
    if(t->nchildren == 0) 
        add_leaf_to_list(t);
}
reverseTreeandReturnListwithLeafNodes(node *root) {

reverse_tree(root);
}

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

ooh I hate those who maintain this website..
if you leave empty lines in the code it always look crapy when posted..

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

Well I needed java, I posted something below. Your approach seems correct. Just that I was having difficulty in Java using List. Syntax issues.
This looks like C but thanks anyways

- chaos April 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes
I'm not sure the recursion is correct... If your return statement is if t==0 which effectively means the passed in variable t->child[i] doesn't exist from the previous call. Then how can you access t->child[i]->child[0]? my purposed code: {{{reverseTree(node *root, node parent) { if(*root) { for(int i = 0; i < root->child.size(); i++) { reverseTree(root->child[i], *root); } root->child[0] = parent; add_to_list(root->child[0]); } } - rdo April 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes
{{{reverseTree(node *root, node parent) { if(*root) { for(int i = 0; i < root->child.size(); i++) { reverseTree(root->child[i], *root); } root->child[0] = parent; add_to_list(root->child[0]); } } - rdo April 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I solved it using a recursive approach. Will post the solution later.

Thanks!

- chaos April 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
- chaos April 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c++ code:
-------------

class NODE{
public:
    string Label
    vector<NODE*> children;
};

vector<Node*> 
reverseTreeandReturnListwithLeafNodes(NODE* n)
{
    vector<Node*> roots;
    reverseTree(roots, n);
    return roots;
}
NODE*
reverseTree(vector<Node*>& roots, NODE* n)
{
    vector<NODE*>& chldrn = n->children;
    if (chldrn.empty())  {
        roots->push_back(n);
        return n;
    }
    vector<NODE*> children = chldrn;
    chldrn.clear();
    vector<NODE*>::iterator iter = children.begin();
    for (; iter != children.end(); ++iter) {
        NODE* child = *iter;
        NODE* p = reverseTree(child);
        p->children.push_back(child);
    }

    return n;

}

- dumbhead April 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry I made a small mistake. it should be NODE* p = reverseTree(n); instead of NODE* p = reverseTree(child); reposting the correct code:

class NODE{
public:
    NODE(string d) : data(d) {}
    string data;
    vector<NODE*> children;
};
vector<NODE*>
reverseTreeandReturnListwithLeafNodes(NODE* n)
{
    vector<NODE*> roots;
    reverseTree(roots, n);
    return roots;
}
NODE*
reverseTree(vector<NODE*>& roots, NODE* n)
{
    vector<NODE*>& chldrn = n->children;
    if (chldrn.empty())  {
        roots.push_back(n);
        return n;
    }
    vector<NODE*> children = chldrn;
    chldrn.clear();
    vector<NODE*>::iterator iter = children.begin();
    for (; iter != children.end(); ++iter) {
        NODE* child = *iter;
        NODE* p = reverseTree(roots, child);
        p->children.push_back(n);
    }
    return n;

}

- dumbhead April 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What does reverse an n-ary tree mean?

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

i think "p->children.push_back(n);" should be
p->children.push_back(p);

- temp April 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Will this work. In order preorder traversal o / p to list then reverse list

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

I think this can be solved using BFS.

- hidden April 30, 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