Amazon Interview Question


Country: India
Interview Type: In-Person




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

class NAryNode
{
int data;
ArrayList<Integer> children;

NAryNode(int data)
{
this.data=data;
children=new ArrayList<Integer>(GIVEN_INT);
}
}

int rollUpTree(NAryNode root)
{
if(root == null)
return -1;
if(root.children.size()==0)
return root.data;
int count=root.data;
for(int i=0;i++;i<n)
{
count=count+rollUpTree(root.children(i));
}
return count;
}

- Vyshnavi June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

for(int i=0;i++;i<n)
{
count=count+rollUpTree(root.children(i));
}

hey what is "n" here?

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

'n' is number of children of root.

- sumeet.kukkar November 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How will you find number of children of root ? I think we need to traverse tree for that but I don't know how to traverse n-ary tree. That is why I came here.

- Priya September 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer in C
==========
Lets there is n-ary Tree
#define CHILD (n)

struct node
{
int data;
int childCount;
node * childrens[CHILD];

};

int rollUpTree(node *root)

{ int i=0,nodeData=0;
if(root==NULL)
return -1;
if(root->childCouor(intnt==0)
{
return root->data;
}
else
{
nodeData=root->data;
for(i=0;i< root->childCount;i++)
{
nodeData = nodeData + rollUpTree(root->children[i]);
}
root->data=nodeData;
return nodeData;
}
}

- prateekjain12@gmail.com June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not a exact roll up of the tree. Reverse BSF order must be followed. The sum of node values in level n is added to nodes in level n-1

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

nodeData=root->data;
for(i=0;i< root->childCount;i++)
{
nodeData = nodeData + rollUpTree(root->children[i]);
}
root->data=nodeData;
return nodeData;

This part will return the sum of sub tree nodes which will be added in current node. so I think it works fine.

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
 Given an n-ary tree (i.e. it can have max n children). 
 One need to roll up the tree from leaves back to root such that the 
 data of root will be the sum of root's data and sum of all its children data
 */

namespace Fundey_ReverseBFS
{
    class Node
    {
        public Int32 data;
        public Int32 childSum;

        public List<Node> arr = new List<Node>();

        public Node(Int32 d)
        {
            this.data = d;
         }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Node n1 = new Node(10);
            Node n2 = new Node(20);
            Node n3 = new Node(30);
            Node n4 = new Node(40);
            Node n5 = new Node(50);
            Node n6 = new Node(60);

            n1.arr.Add(n2);
            n1.arr.Add(n3);

            n2.arr.Add(n4);
            n2.arr.Add(n5);

            n3.arr.Add(n6);

           GetChildSum(n1);

            
            Console.ReadLine();
        }

        public static Int32 GetChildSum(Node root)
        {
            Int32 sum=0;

            if (root == null)
                return 0;

            //Int32 sum = GetChildSum(root.left) + GetChildSum(root.right) + root.data;
            
            Int32 childNum = 0;
            while (root.arr.Count >0 && childNum < root.arr.Count)
            {
                sum = sum + GetChildSum(root.arr[childNum]);
                childNum++;
            }

            sum = sum + root.data;

            root.childSum = sum;


            Console.WriteLine("Node = " + root.data + "     Child level sum = " + (sum - root.data)); 
            return sum;
        }

    }
}

- Lazy June 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This piece of code should sum up the childrem to root. the questions sounds simple. Am I missing something?

public void sumTreeNodes (Node n) {
sum = sum + n.getValue();
if (n.getNodeList()!= null && n.getNodeList().size() > 0) {
for (Node node: n.getNodeList()) {
sumTreeNodes (node);
}
}

}

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

long getSum(Node node) {
    long sum = 0;
    for ( Node n: node.getNodeList()) {
        sum += getSum(n);
    }
    node.value += sum;
    return node.value;
}

- sqw June 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can do it through modified postorder traversal
int postorder(struct node *head,int sum)
{
if(head != NULL)
{
int s1= postorder(head->left,sum);
int s2= postorder(head->right,sum);
head->data = s1+s2+ head->data;
return(node->data);
}
return 0;
}
call(root,0) from main.. i hope i am clear in explaining

- rockstar November 09, 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