Amazon Interview Question for Software Engineer / Developers


Team: AWS
Country: United States
Interview Type: Phone Interview




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

void avg(node *root,int *sum,int *count)
{
     if(root)
     {
             *sum  = *sum + root->data;
             *count = *count + 1;
             cout<<root->data<<" "<<*sum<<" "<<*count<<endl;
             avg(root->left,sum,count);
             avg(root->right,sum,count);
     }
     else return;
}

- mandeep March 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Implement the two function , one call the another with the sum and counter pointers. In the second function iterate the tree in inorder and calculate sum and no of element.
Return to the main function now sum and counter pointer has their values so calculate the avg and return.

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

employ typical dfs/bfs, with little modification to count number of nodes encountered and sum of the values of node.
When traversal ends, get the average

- varun March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(log n). Same as counting number of nodes and additional step is to copy the value. After the function returns take the avg
print avg (node* root)
{
int value = 0;
int data = 0;
if(node==null)
{
printf("%d",data/value);
return;

}
else
{

data+=node->left+data;
value++;
data+=node->data;
value++;
data+=node->right+data;
value++

avg(node->left);
avg(node->right);
}

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

LOL you think this is O(logn)??? Go read data structures once again

- LOL March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Rascala anonymous,
for getting the value in all the nodes, and counting the number of nodes will take O(n) time.
And you are calculating the average in O(lgn)
Rascala pussy, stay in ur limits....
only Rajnikanth can compute this in O(1) time... Mind it! ;)

- Rajnikanth March 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude , Seriously (log n) , U mean u can compute the average of n numbers by not even looking at them , Hail U.

On a serious note , do any kind of traversal and accumulate the sum and count of the nodes and compute the average with complexity O(n).

- Vinayak March 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good solution

- guest August 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

print avg (node* root)
{
int value = 0;
int data = 0;
if(node==null)
{
printf("%d",data/value);
return;

}
else
{

data+=node->data;
value++;
avg(node->left);
avg(node->right);
}

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

do you think, it will work ? how calculated data is being passed from one method to other.

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

stored in value

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

int value = 0;
int data = 0; should be static

- Sourabh April 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Easiest way is to write 2 function. 1 to calculate sum and another to calculate num of element and then divide.

If want to do in a single method then.
1- do post order traversal.
2- pass num of element, num, avg to the calling function.

int avg (Node root,int arr[])
{
if (root.left != null){
avg(root.left,arr);

if (root.right!= null){
avg(root.right,arr);

arr[0] += root.data;
arr[1]+=1;
arr[2] = arr[0]/arr[1];
return arr[2];
}

avg(

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

Use 2 variables to keep track of sum and count of nodes respectively.
Do a traversal of entire tree and return sum/count at end.

int getAverage(Node *root) {
    int sum = 0;
    int count = 0;
    
    traverseTreeForAverage(root, &sum, &count);
    
    if (count == 0) {
       return 0;
    } else {
       return sum/count;
    }
}

void traverseTreeForAverage(Node* node, int *sum, int *count) {
    if (node != null) {
       *sum += node->value;
       *count += 1;

       traverseTreeForAverage(node>left);
       traverseTreeForAverage(node>right);                           
    }
}

- Tyronne M March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int getAverage(Node *root) {
    int sum = 0;
    int count = 0;
    
    traverseTreeForAverage(root, &sum, &count);
    
    if (count == 0) {
       return 0;
    } else {
       return sum/count;
    }
}

void traverseTreeForAverage(Node* node, int *sum, int *count) {
   if (node != null) {
      *sum += node->value;
      *count += 1;

      traverseTreeForAverage(node>left, sum, count);
      traverseTreeForAverage(node>right, sum, count);
   }
}

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

int avg(NODE *head) // helper function that recursively calls the sume function
{
int count=0;
int sum=sume(head,&count);
return sum/count;
}

int sume(NODE *head,int *p)
{

if(head==NULL)
{
return 0;
}
else
{
*p=*p+1;
}
return(sume(head->right,p)+sume(head->left,p)+head->value);
}

I have made the sum return from the sume function as a return value, wheras the count is intialized as 0 and passed in recursive function by reference.

You divide the sum with count and return the avg from the helper function

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

int avg(NODE *head)             // helper function that recursively calls the sume function
{
	int count=0;
	int sum=sume(head,&count);
	return sum/count;
}

int sume(NODE *head,int *p)
{
	
	if(head==NULL)
	{
		return 0;	
	}
	else
	{
		*p=*p+1;
	}
	return(sume(head->right,p)+sume(head->left,p)+head->value);
}

I have made the sum return from the sume function as a return value, wheras the count is intialized as 0 and passed in recursive function by reference.

You divide the sum with count and return the avg from the helper function

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

u can implement thsi in only one function :)
just use one extra vraible as a para,mter

intialy pass c as a value zero:
int sumavg(struct node *t,int c)
{
static int n;
int sum;
if(!t)
return 0;

sum=t->info+sumavg(t->left,1)+sumavg(t->right,1);
if(c==1)
return sum;

return (sum/n);
}

- geeks March 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Passing the sum and count variables as parameters seem to be wrong since we don't have a track when the recursion has traversed all the nodes.
Keep sum and count as global variables or class variables in java.
No need to pass any parameters.

Time : O(n)

// sum and count are class variables.
// sum = 0; count = 0; before calling avg.
// call this function as avg(rootNode).
// after the function call, avg = sum / count;

public static void avg(Node node)
    {
      if(node == null)
      {
        // System.out.println(sum + " " + count);
        return;
      }
      else
      {
        sum += node.value;
        count++;
        avg(node.left);
        avg(node.right);
      }
    }

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

Traversing Nodes in the tree level order and summing up value as you visit. Additional Q required to store each level.

Queue<Node> queue = new LinkedList();
Node flagNode = new Node(00);
int avg = 0;
int sum =0;
int count = 0;
queue.add(rootNode);
queue.add(flagNode);
while (!queue.isEmpty()) {
Node node = queue.poll();
// System.out.print("While Condition Values "+node.value);
if (node != flagNode) {
sum = sum + node.value;
System.out.print(node.value+ " ");
//System.out.println("Sum"+sum);
count++;
if (node.leftChild != null) {
queue.add(node.leftChild);
//sum = sum + node.leftChild.value;
}
if (node.rightChild != null) {
queue.add(node.rightChild);
// sum = sum + node.rightChild.value;
}
} else if (!queue.isEmpty()) {
// System.out.println("Adding Flag");
queue.add(flagNode);
}
}
System.out.print("Sum"+sum);
avg = sum / count;
System.out.print("Avg"+avg);
}

- Sindhuja.Narasimhan April 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

guys , all your algorithms are correct, and they are all same , traverse the tree and sum the values and keep a variable for count,

thats simple logic of all the replies above,

Amazon needs something more better than that,
I also said the same algorithm in the interview , thay are expecting something else as a response and not as simple as that.

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

float avg(NODE *node){
	static int sum = 0;
	static int count = 0;
	
	if(node){
	sum += node->data;
	count++;
	avg(node->left);
	avg(node->right);
	}

	if (count)
		return sum / count;
}

- Sourabh Kumar Choudhary April 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int avg(Node n)
{
if(!n) return 0;

if(isLeaf(n)) return (n->data);
else
return ( avg(n->left) / 2 + avg(n->right)/2 )
}

- kv391 April 07, 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