Amazon Interview Question for Software Engineer / Developers






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

create an array sum[] for storing the content.. initialised to 0
find the maximum depth of the tree...
make an inital call to fun(root,depth)
function fun is :

void fun (node *root, node level){
     sum[level]+=root->data;
       fun(root->left,level--);
       fun(root->right,level++);
}

takes O(n) complexity ..

- sukhmeet July 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If level represents depth, then this computes horizontal sum :) but the idea is the same.

- airfang613 July 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@airfang613 : it will print the vertical sum and not the horizontal, work with some example

- Ashupriya June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's correct, I don't know what I was thinking when I made the comment.

- airfang613 June 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Slightly modified

void verticalSum( TNode* node, int *arr, int Level)
{
  if (node==NULL) return;  

  verticalSum(node->Left,arr, Level-1);
  arr[Level]=arr[Level]+node->value;
  verticalSum(node->Right,arr,Level+1);  
}

- hari@hbiz.in August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

i think level-- and level++ should be replaced with level-1 and level+1, respectively.

- explorer July 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Anonymous
This is a standard question. Google for vertical sum of a tree

- Ray July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think level-- and level++ should be replaced with level-1 and level+1, respectively.

- explorer July 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with explorer.. :-P

- barry lance leo July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with explorer.. :-P

- barry lance leo July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with explorer.. :-P

- barry lance leo July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we take an array to store the vertical sums, it will be very difficult to decide the length of the array and the starting position of the root. I would take a doubly linked list to store the vertical sums and add nodes to left and right if required.

- anonymous October 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Total array size should be 2 ^ Depth + 1
// Initialize the array with 0
//Call fun (head, size/2)

int[] sum = new int[size];
void Fun(Node head, int index)
{
Fun(head.Left, index -1);
sum[index] = head.Data;
Fun(head.Right, index + 1);
}

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

void verticalSum( struct tree * root, int a[], int pivot)
{
if(root== NULL)
return;

a[pivot]= a[pivot] + root->data;

if( root->left)
{ pivot--;
verticalSum(root->left,a,pivot);
}
if( root->right)
{ pivot++;
verticalSum(root->right,a,pivot);
}

}
int main()
{
int a[100];
for(i= 0; i < 100; i++)
a[i] = 0;

vericalSum( root, a, 10);


}

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

^^This will avoid negative indices..

- Anonymous December 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution with a deque ....

deque<int> dq;
int cursor = -1; /* cursor is the index of current node in deque */

void GetVerticalSum( Node *root )
{
if ( root == NULL )
return ;

if ( cursor == -1 ) /* reach left , add elem in deque head */
{
dq.push_front ( root->data );
cursor = 0;
}
else if ( cursor == dq.size() ) /* reach right, add elem in deque tail */
dq.push_back( root->data );
else /* No need to add new elem in deque */
dq[ cursor ] += root->data ;


/* traversal */
if ( root->left )
{
cursor--;
GetVerticalSum( root->left );
cursor++;
}
if ( root->right )
{
cursor++;
GetVerticalSum( root->right );
cursor--;
}
}

- iatbst January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

can u explain how ur sum Array is constrcuted for the given tree. Question is not clear.

- Anonymous July 21, 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