Amazon Interview Question for Software Engineer in Tests

Country: United States
Interview Type: Phone Interview

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

function balanced(Node *root){
if (root == NULL)
return 0;

int left = balanced (root->left);
int right = balanced (root->right);

if (left == -1 || right == -1) // if any subtree is unbalanced
return -1;
else if (abs (left-right) <= 1) // this subtree is balanced
return left+right+1; // return number of nodes
else {
return -1;
}
}

Complexity : O(n)

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

Define balanced.

Red-black trees are considered balanced, for instance...

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

Balanced tree is
(1)an empty tree
(2)left subtree is a balanced tree && right subtree is a balanced tree &&
abs(height difference of the two subtrees) <= 1
Following is C++ code：

bool isBalanced(Node* root)        //interface to outside
{
int height;
return isBalanced(root, height);
}
bool isBalanced(Node* p, int& height)
{
if(p == NULL){
height = 0;
return true;
}

int leftHeight, rightHeight;
if(isBalanced(p->left, leftHeight)   &&
isBalanced(p->right, rightHeight) &&
abs(leftHeight - rightHeight) <= 1
){
height = 1 + max(leftHeight, rightHeight);
return true;
}
return false;
}

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

NO NO NO.

That is the definition of height balanced tree. (Like AVL).

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

int isBalanced(Node *root){
int hl,hr;
if(root == NULL)
return 1;

hl = height(root->left);
hr = height(root->right);

if(abs(hl-hr) <=1 && isBalanced(root>left) && isBalanced(root->right))
return 1;

return 0;
}

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

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

A balanced tree is one in which
height of left subtree - height of right subtree is atmost 1.

A naive approch would be:
1. start from root
2. while an unvisited node exists, check if the subtree rooted at that node is balanced or not
3. if all such subtrees are balanced then return true else return false

int isBalanced(Node *root)
{
if(root==NULL)
return 1;
int lHeight = height(root->left);
int rheight = height(root->right);

if(abs(lHeight-rHeight)  > 1)
return 0;
return isBalanced(root->left)&&isBalanced(root->right);
}

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

This approach checks the height for every node. So time complexity is O(nlgn)
To reduce the time complexity we can modify the height function. We can use a flag which will be set at the level the heights mismatch. So while calculating the height of the tree it will also check if the tree is balanced or not.

int height(Node* root, int* balanced) //initially balanced = 0
{
if(root==NULL)
return 0;
int lHeight = height(root->left,balanced);
int rHeight = height(root->right,balanced);
if(abs(lHeight-rHeight)>1)
*balanced = 1;  //setting the flag
return max(lHeight,rHeight)+1
}

if balanced = 1 => Tree is not height balanced
This will reduce the time complexity to O(n)

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

NO NO NO.

That is the definition of height balanced tree. (Like AVL).

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.

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.