Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

it can be done with a modified post-order traverse. To simplify things, i'll put it in recursive style:

int pot(BinaryTree *p) {
	int count = 0;
	if (!p) return count;
	count += pot(p->left);
	count += pot(p->right);
	if (count == k) cout<<&p<<endl;
	return count;
}

- suzker January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(N) and O(1) for storage, of course, the recursive implementation is kinda expensive while the tree is deep because of the stacks stored while making recursive calls.

- time complexity January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@suzker-U r logic is right but the code u have provided will not print anything for any input

- saumya.tyagi6 January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The idea is right. You should add:

count = (count >= 1) ? count : 1;

before the if clause for it to work.

- IvgenyNovo January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@IvgenyNovo, thanks for pointing out, this is critical.

- suzker January 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Complexity O(n)

public List getListOfNodes(Node root,int k)
{
  ArrayList<Node> list= new ArrayList<Node>()
  getLeaves(root,list,k);
  return list;
}

public int getLeaves(Node node,ListnNode[] nodeList,int  k)
{
  int numLeafL,numLeafR=0
  if(node.getLeft==null && node.getRight==null)
  {
    return 1;
  }
  if(node.getLeft)
  {
  numleafL=getLeaves(node.getLeft);
  
  }
  if(node.getRight)
  {
  numleafR=getLeaves(node.getRight);
   }
   
   
   currentLeafcount=numLeafL+numRight;
   if(currentLeafCount==k)
   {
   nodeList.add(node)
   }
   return currenLeafCount;
   
   
}

- satyajeettripathy January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

We have to use Bottom up approach
1. Traverse the tree in postorder manner.
2.While traversing keep count of the leaf nodes
3. add nodes who have k leaves to list
4. At the end LIst will contain the ans

int getNodeWithKLeaves(Node root,int k,LinkedList<Integer> list)
   {
       if(root==null)
           return 0;
       int x=0;
       x+=getNodeWithKLeaves(root.left, k, list);
       x+=getNodeWithKLeaves(root.right, k, list);
       
       if(root.left==null&&root.right==null)
           x++;
       if(k==x)
           list.add(root.info);
       return x;
   }

- saumya.tyagi6 January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

o(n) time complexity

- saumya.tyagi6 January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why is
if(root.left==null&&root.right==null)
x++;
statement required just do
if(x==0) x++; at the same place

- kkr.ashish January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@kkr.ashish U checked x==0 which means there is no leaf node to that particular node that means it is a leaf node itself and i m also checking the same so it is the two different ways of doing same thing

- saumya.tyagi6 January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't work if I give k=0; it return all nodes. Shouldn't it return all leaf nodes for k=0?

- Vishal January 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have a recursive method called getNoOfLeaves(). The base case of recursion returns 1 for leaf nodes. During the bubbling up(unwinding) of the recursive calls, sum up the number of leaves from both it's children and check if it equals to the given k. If yes, add the node to a list.

- Murali Mohan January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Complexity O(n)

public int findNodesWithK(Node node ,int k, LinkedList<Node> list){
    	if (node==null){
    		return 0;
    	}else{
    		int count = findNodesWithK(node.left,k,list) + findNodesWithK(node.right,k,list);
    		if (count==k){
    			list.add(node);
    		}
    		return count+1 ;
    	}

}

- Scott January 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int CountLeafeNodes(Node* root)
	{
		if(root==NULL)return 0;
		if(root->left==NULL&&root->right==NULL)return 1;
		return CountLeafeNodes(root->left)+CountLeafeNodes(root->right);
	}

- Gangadhara P January 23, 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