Amazon Interview Question for Software Engineer in Tests






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

int banancedBSTHelper(node* root, bool isBalanced)
{
	int leftHight, rightHight;
	if(root==0) return 0;
	leftHight=banancedBSTHelper(root->left, isBalanced)+1;
	rightHight=banancedBSTHelper(root->right, isBalanced)+1;
	if(ABS(leftHight-rightHight)>1)
		isBalanced=false;
	return (leftHight>rightHight)?leftHight:rightHight;
}
bool isBalancedBST(node* root)
{
	bool isBalanced=true;
	balancedBSTHelper(root, &isBalanced);
	return isBalanced;
}

- socrates July 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Zzzzzz....

- Anonymous July 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

anandtechblog.blogspot.com/2010/12/validate-binary-search-tree-google.html

- Anand July 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ur method test whether a tree is valid or not. It wont chk for balance...stop advertising ur nonsense blog in every ques posted here!!

- Anonymous August 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1

- Anonymous December 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isBalanced = true;
int isTreeBalanced(int level, node* ptr)
{
   if(ptr == null) return level;
   int leftDepth = isTreeBalanced(level+1, ptr->left);
   int rightDepth = isTreeBalanced(level+1, ptr->right);
   if(abs(leftDepth - rightDepth) > 1)
      isBalanced = false;
}

- Anonymous July 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code is wrong...... it compares the levels which will always be same for left and right subtree

- Anonymous November 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int depthOfBT(node *ptr, int level)
{
   if(ptr) return level - 1;
   else
   {
      return max(depthOfBT(ptr->left, level+1), depthOfBT(ptr->right, level + 1));
   }
}

bool isBalanced(node* ptr)
{
   if(ptr!=NULL && abs((depthOfBT(ptr->right) - depthOfBT(ptr->left))  <= 1))
      return true;
   else 
      return false;
}

- Anonymous July 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

PPL please explain your algos rather than coding:

I am suggesting folowing algo.

Traverse a tree in any order & check difference between height of left & right subtree for each non-leaf node if it is -1 0 or 1 for all nodes tree is height balanced.


Now you can see there are too many repetition in top down approach.

try same program with bottom up & keep height of left & right subtree & use them for parent node.

Need clarification please reply

- Rahul D July 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int height(btree *node)
{
if(node == NULL)
	return 0;
return max(height(node->left), height(node->right)) + 1;
}

bool isBalanced(btree *node)
{
if(node == NULL)
	return true;
if(abs(height(node->left)-height(node->right))>1)
	return false;
return isBalanced(node->left) && isBalanced(node->right);
}

- nagarajmailing August 28, 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