## Amazon Interview Question for Software Engineer Interns

Country: United States

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

``````public class Node {
public Node left = null;
public Node right = null;

public Node(Node left, Node right) {
this.left = left;
this.right = right;
}
}

public int maxHeight(Node x) {
if (x == null) {
return 0;
}
return Math.max(maxHeight(x.left), maxHeight(x.right)) + 1;
}``````

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

``````def max_height root
if !root
return 0
end

right = heigth root.right
left = heigth(root.left)

return 1 + [right,left].max

end

puts heigth n1``````

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

post order traversal.
depth(tree) = 1 + max(depth (left-subtree), depth(right-subtree))
depth of leaf node is zero.

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

``````int d(Node node) {

if (node == null) {
return 0;
}

return 1 + Math.max(d(node.left), d(node.right));
}``````

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

Could you please provide the code for it?

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

straightforward O(n) solution:
do InOrder Traversal, and maintain two variables (a max and a counter) as follows: while traversing in depth increment counter (and while walking up decrement it), when you hit a leaf node do a check to see if a counter value is greater than the max value. If so - update max variable, then continue traversing.

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

Can you provide the code for it?

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

``````template <class T>
int CBst<T>::GetHeightOfTree(BstNode<T>* pRootNode)
{
if(NULL == pRootNode )
return -1;
return max((GetHeightOfTree(pRootNode->GetLeftChild())+1),(GetHeightOfTree(pRootNode->GetRightChild())+1));
}``````

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

``````#include <iostream>
using namespace std;

class tree {
public:
tree *left;
tree *right;
int data;
tree(tree *l,tree *r, int d) : left(l), right(r), data(d) { }
};

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

int main() {
tree *root, *buffy, *dummy;
root = buffy = new tree(0,0,1);
for (int i = 2; i < 100; i++) {
dummy = new tree(0,0,i);
if (rand() % 2) {
buffy->left = dummy;
buffy = buffy->left;
} else {
buffy->right = dummy;
buffy = buffy->right;
}
}
cout << "depth " << findMaxDepth(root);
return 0;``````

}

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

c++ answer with random tree fill.

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

Level order traversal should be used for it

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

int depth_tree2(struct btree **b) {
if((*b)->left == NULL && (*b)->right == NULL )
return 0;

int l=0, r=0;

if((*b)->left != NULL)
l = depth_tree2( &((*b)->left)) + 1;
if((*b)->right != NULL)
r = depth_tree2( &((*b)->right)) + 1;

if(l>r)
return l;
else
return r;
}

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

Your can use logic for printing of nodes level wise is this question.

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.