Amazon Interview Question for Software Developers


Country: United States
Interview Type: Written Test




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

In a binary tree there can be any value on any node, so we need to keep track of the Min. value found so far for that depth branch and propagate it until you reach the leafs, be aware not to count duplicates in the way back, a HashSet would help to avoid that,
and the final HashSet will hold all Nodes which are lesser than all their possible parents.

public static int countNumLesserNodes(Node<Integer> root) {
	if(root == null) {
		return 0;
	}
	Set<Node<Integer>> set = new HashSet<>();
	countNumLesserNodes(root.left, root.data, set);
	countNumLesserNodes(root.right, root.data, set);		
	return set.size();
}

public static void countNumLesserNodes(Node<Integer> root, int pVal, Set<Node<Integer>> set) {
	if(root == null) {
		return;
	}
	if(root.data < pVal) {
		set.add(root);
		pVal = root.data;
	}				
	countNumLesserNodes(root.left, pVal, set);
	countNumLesserNodes(root.right, pVal, set);		
}

- guilhebl April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I thought we need to take all the node values at the upper "level" that means BFS and also keeping track of min. Am I missing something?

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

To further elaborate on sreekar's point:
I think what's wrong in this soln is that the call to the children nodes should take the max b/w root.data and pVal.

There's no mention of upper "level" in the problem stmt, so just looking at all upper nodes on each side should suffice.

- deepdawg August 02, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

int countNode(root) {
	if(root == null) 
		return 0;
	int cnt = 0;
	if(root.left != null && root.value > root.left.value)
		cnt += 1 + countNodes(root.left);
	if(root.right != null && root.value > root.right.value)
		cnt += 1 + countNodes(root.right);
	return cnt;

- timpham2003 April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is wrong and it has 2 errors: 1 it counts duplicates and 2 it only counts direct parents and childs, you need to consider all the parents above on that branch, for example suppose you have a case as:

1
\
3
\2

your solution would wrongly consider 2 as one such node, but it's higher than a parent above on his branch.

- guilhebl April 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can do this with a quick depth-first recursion.

In python:

def get_valid_nodes(root, curr_sum):
    if not root:
        return 0
    left_count = get_valid_nodes(root.left, curr_sum + root.val)
    right_count = get_valid_nodes(root.right, curr_sum + root.val)
    root_valid = 1 if root.val < curr_sum else 0
    return root_valid + left_count + right_count

What it does is keep track of the sum of values in the traversal to the current node in the tree. If it is the bottom of the tree, it returns 0. Otherwise, it recurses on its left and right child, then adds their values to 1 (if the curr node has a val less than the above sum) or 0 and returns that value.

runs in O(n log n)

- Javeed April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class binaryProcessor{
	int count =0;
	public void countNodesLessThan(Node root) {
		countNodesLess(root.left,root.value);
		countNodesLess(root.right,root.value);
		
	}
	private void countNodesLess(Node node, int value) {
		if(node == null){
			return;
		}
		if(node.value<value){
			count++;
		}
		countNodesLess(node.left,node.value);
		countNodesLess(node.right,node.value);
	}

}

- varun April 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

even if the root to leave path is 2->3->1 and should be 1 as last node fulfills the condition (i guess)

FindNumberOfNodes(N,max)
{
  if(N==null) return 0;
  bool isCoditionSatisfied = N<max;
  l = FindNumberOfNodes(N.Left, (isCoditionSatisfied) ? N.data : max)
  r = FindNumberOfNodes(N.Right, (isCoditionSatisfied) ? N.data : max)
  return l + r + ((isCoditionSatisfied) ? 1:0);
}

- aditya.eagle059 April 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what about the case where you can have:
1\3\2 ?

- guilhebl April 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we can modify the nodes, have a property "count" in each node indicating how many descendent nodes have values less than that node's value.

Insert: increment count property of nodes as you traverse the tree to find an appropriate insertion spot

Delete: decrement count property of nodes as you traverse the tree to find the node to delete. This is a little trickier now as you need to also update the count property of the node that will replace the deleted node when deleting a node with 2 children; this is mainly only efficient if we're dealing with a BST.

- JW April 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int countNodes(Node *root) {
     if(root == null || root->left==null && root->right==null) {
		return 0;
	}

	int count += countNodes(root->left);
	count +=countNodes(root->right);
   
	if(root->data > root->left->data) count++;
	if(root->data > root->right->data) count++;     
  
	return count;

}

- Guru May 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int countMinNodes(Node node, int min) {
        boolean flag = false;
        if (node == null) {
            return 0;
        } else {
            if (min == Integer.MIN_VALUE) {
                min = node.getValue();
            } else {
                flag = min > node.getValue();
                min = min > node.getValue() ? node.getValue() : min;
            }
            if (flag) {
                return 1 + countMinNodes(node.getlChild(), min) + countMinNodes(node.getrChild(), min);
            } else {
                return countMinNodes(node.getlChild(), min) + countMinNodes(node.getrChild(), min);
            }

        }

    }

- Vivek Verma May 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We simply need to pass down the minimum value of all Nodes above the current Node, which is updated at each level of the stack frame. Then, return the # Nodes below you which are less than their upper Nodes, plus one if current Node is less than its upper Nodes

struct Node {
	Node* left;
	Node* right;
	int datum;
};

int getNumNodesLowerThanParents(Node* root) {
	if(!root) return 0;
	return getNumNodesLowerThanParentsHelper(root->left, root->datum) +
		  getNumNodesLowerThanParentsHelper(root->right, root->datum);
}

int getNumNodesLowerThanParentsHelper(Node* root, int minAbove) {
	if(!root) return 0;
	int thisAddition = 0;
	if(root->datum < minAbove) {
		minAbove = root->datum;
		++thisAddition;
	}
	return getNumNodesLowerThanParentsHelper(root->left, minAbove) +
		  getNumNodesLowerThanParentsHelper(root->right, minAbove) +
		  thisAddition;
}

- Josh August 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{public int countnodes(Node node,int key)
	{
		int count=0;
		if(node!=null)
		{
			if(node.key>key)
			{
				count++;
			}
			count=count+countnodes(node.left,key);
			count = count+countnodes(node.right,key);
		}
		
		return count;
	}

}

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

public int countnodes(Node node,int key)
	{
		int count=0;
		if(node!=null)
		{
			if(node.key>key)
			{
				count++;
			}
			count=count+countnodes(node.left,key);
			count = count+countnodes(node.right,key);
		}
		
		return count;
	}

- Arpit August 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int countNodesLesserThanUpperNodes(AVLTree.Node node, int maximum){
return node == null? 0 : ((node.data < maximum? 1 : 0) + countNodesLesserThanUpperNodes(node.left,Math.min(maximum, node.data) +
countNodesLesserThanUpperNodes(node.right,Math.min(maximum, node.data))));
}

- Sandy April 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int countNodesLesserThanUpperNodes(AVLTree.Node node, int maximum){
return node == null? 0 : ((node.data < maximum? 1 : 0) + countNodesLesserThanUpperNodes(node.left,Math.min(maximum, node.data) +
countNodesLesserThanUpperNodes(node.right,Math.min(maximum, node.data))));
}

- Sandy April 05, 2016 | 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