Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

two steps
firstly, find right node's parent
secondly, change parent to the right node's child

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

That won't work. The answer is

* Find the node using DFS. This is to get the path to the node from root.
* Reverse all the links from the node to the root. like make the parent node as the child node.

That's all. Very simple.

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

Why do u need DFS here? Ur right hand is already on target node i.e. You have pointer to that node as an input. Now,

Node* node_to_root(Node* root, Node* new_root)
{
- Remove the link to new_root from the list of children of root.
- Insert root into the list of children of new_root. If new_root already has some children they still continue to be children
}

- Sani October 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, I don't think the problem is that simple. An N-ary tree means any node of the tree will have at max N children. Suppose the intermediate node which we are making the new root already has N children. Then if we make its earlier parent also its children, then the new root will have N+1 children, which violates the N-ary tree property.
Besides to reverse the pointers we need to have parent pointers, which means we have to change tree structure as well.

- Tiscaao! October 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reposting the above comment:
Well, I don't think the problem is that simple. An N-ary tree means any node of the tree will have at max N children. Suppose the intermediate node which we are making the new root already has N children. Then if we make its earlier parent also its children, then the new root will have N+1 children, which violates the N-ary tree property.
Besides to reverse the pointers we need to have parent pointers, which means we have to change tree structure as well.

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

I guess it is that simple only, i.e. all that is needed to be done is to reverse the pointers of the node from root to the node that is being held in the right hand, that's all. And important thing is that the condn of n-ary tree will not break because for each node whose pointer is being reversed is actually loosing one child is also gaining one child as well. But yes for that one particular node i.e. the one being held in right hand, it could be an issue is if it is already having n children.

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

adding to the above: the new root will have one extra child( its parent became child) and the old root will have one less child ( as one of its child becomes parent)

- choupsey November 17, 2011 | 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