Amazon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

Reverse inorder traversal. right->root->left. Calculate the sum and pass to left node.

public int sumGreaterNodes(Node node, int sum){
    if (node == null)
      return sum;
    
    int rs = sumGreaterNodes(node.getRight(), sum);
    int cs = node.getKey() + rs;
    node.setKey(cs);
    int ls = sumGreaterNodes(node.getLeft(), cs);
    return ls;
  }

sumGreaterNodes(root, 0);

- geekyjaks May 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Consider below BST :
12
/ \
10 24
/ / \
5 14 36
/ \ \
4 6 18
/ \
16 21

With above algo, can you give the answer it will give for nodes with values, 12, 14 and 6?

(In-order traversal is 4, 5, 6, 10, 12, 14, 16, 18, 21, 24, 36)

- nilukush May 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@geekyjaks, I don't think your return statement is correct here.

- linusyang2016 May 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above code is tested. You can ignore the final return statement. The 'return' value is used inside the function. Here is the inorder traversal after sum,

before: 4->5->6->10->12->14->16->18->21->24->36
after: 166->162->157->151->141->129->115->99->81->60->36

- geekyjaks May 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anybody can tell if this approach was good enough ?

- ur.devesh May 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think of the mistake that had my approach is having that when we traverse to left node we should pass the summation that is coming from right side of the node,because all value of left node is less that the node in the right of the parent .

- ur.devesh May 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I also considered the reverse in-order traversal that geekvjaks mentioned. Here's my version:

void sumGreaterNodes(Node n, IntWrap sum) {

	if (n.right != null)
		sumGreaterNodes(n.right, sum);

	n.greaterValuesSum = sum.value;
	sum.value += n.value;

	if (n.left != null)
		sumGreaterNodes(n.left, sum);
}

class IntWrap {
	public int value;
}

- Michael.J.Keating May 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is meant by "You have to compute for each node the summation of all nodes whose value is greater than the current node." ?

Do I have to find all nodes whose value is less than the sum of its children?

- Anonymous May 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It means if my current node is 4 , then find the summation of all the nodes which is greater then 4 . This process need to be done to all the nodes.

- ur.devesh May 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node:
    def __init__(self, value):
        self.left = None
        self.right = None
        self.val = value

class Tree:
    def __init__(self):
        self.root = None

    def add(self, value):
        node = Node(value)
        if not self.root:
            self.root = node
        else:
            current = self.root
            while (True):
                if current.val < node.val:
                    if not current.right:
                        current.right = node
                        break
                    current = current.right 
                else:
                    if not current.left:
                        current.left = node
                        break
                    current = current.left

    def helperSum(self, current, value): 
        s = 0 
        if current:
            if current.left and current.left.val > value:
                s = s + self.helperSum(current.left, value)
            if current.val > value:
                s = s + current.val
            if current.right:
                s = s + self.helperSum(current.right, value)
        return s

    def getSum(self, value):
        return self.helperSum(self.root, value)

- Pat May 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

vector<Node> traverse(Node *root, vector<Node> ls){
ls.push_back(*root);
if(root->left != NULL ){
ls = traverse(root->left,ls);
}
if(root->right != NULL ){
ls = traverse(root->right,ls);
}
return ls;
}
struct myclass {
bool operator() (const Node &i, const Node &j) { return (i.data < j.data);}
} myobject;
void getNodeSum(Node *root, vector<Node> ls){
ls = traverse(root,ls);
// sort ls in ascending order
sort(ls.begin(),ls.end(),myobject);
int s = 0;
for(int k = ls.size()-1; k >= 0 ; k--){
s += ls[k].data;
ls[k].sum = s;
}
for(vector<Node>::iterator it = ls.begin(); it != ls.end(); ++it){
Node n = (Node)*it;
cout<<"Node "<<n.data<<" has sum = "<<n.sum<<endl;
}
}

- Anonymous May 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

An inorder traversal starting from the rightmost node.
Right Node- Node- Left Node.

public static int inorderSum(Tree t,int sum)
{
    if(t!=null)
    {
        sum=inorderSum(t.right,sum);
           sum=sum + t.data;
       t.data=sum;
        System.out.print(t.data+" ");
   	sum=inorderSum(t.left,sum);
    }
	return sum;
}

- wolfengineer June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about just using BFS and summing all the nodes whose value is greater that the given nodes value.

Another approach: Modify preorder traversal such that root.left is only sent in recursive call if it is greater that the given node's value.

- keepitsimple September 19, 2014 | 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