Amazon Interview Question for Software Engineer / Developers






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

{
public int sumNodeLevels(Node node, int level){
if(node != null){
int result = ++level;
// System.out.println("Node - " + node.info + ", " + level);
result += sumNodeLevels(node.left, level);
result += sumNodeLevels(node.right, level);

return result;
}

return 0;
}
}

- vaibhav.iit2002 March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a doubly linked list.
Do any order(pre, in, or post order) traversal and while we move left in the tree add the value in the current node to the current node in the doubly linked list and move left and vice versa for the right movement.

Now the final values in the doubly linked list will contain the sum values.

Also if we know the width of the tree, we can then create a integer array and then start from the root of the tree and corresponding index of the root in the tree width. then add the value in the root to the array[index]. Now if you move left decrement index or if you move right increment index.

now the final array will have the vertical sum of the tree.

- Thiyanesh March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Anonymous, your definition of level is not clear - Please clarify??

For example:
What is level 3 : 1,5,6

- Brian March 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what do you mean by
Level is defined as: Level 1: 4
Level 2: 2
level 3 : 1,5,6
level 3: 3
level 7: 7

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

Can we not use BFS for printing the sum at each level ?

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

if you cannot describe a question don't post it...

- @question poster April 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if you cannot describe an question don't post it...

- @question poster April 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just do like this way and u will find solution here :)
void verticalSum(struct node *R,int level,int sum[])
{
if(!R)
return;
sum[level]=sum[level]+R->data;
verticalSum(R->left,level-1,sum);
verticalSum(R->right,level+1,sum);
}

but first you have to find the no of levels at the left of the root node
for this to be done u can
int Nofleft(struct node *R)
{
if(!R)
return 0;
else
return(Noleft(R->right)+1);
}
}
and thus where u called this ucan then pass and call vertical sum by passing level=level+no of nodes at the left of the root node
i think it is prety easy :)

- geeks August 01, 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