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;
}

- techinterviewquestion.com January 31, 2016 | Flag Reply
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

- fasil January 31, 2016 | Flag Reply
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.

- __xy__ February 01, 2016 | Flag Reply
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));
}

- er.vishalmehta83 January 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Could you please provide the code for it?

- Sharanya February 09, 2016 | Flag
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.

- zr.roman January 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can you provide the code for it?

- Anonymous February 09, 2016 | Flag
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));
}

- chitaranjan2505 February 01, 2016 | Flag Reply
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() {
	// your code goes here
	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;

}

- utkubulkan February 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

c++ answer with random tree fill.

- utkubulkan February 03, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Level order traversal should be used for it

- Anonymous April 20, 2016 | Flag Reply
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;
}

- kbkunalb May 15, 2016 | Flag Reply
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.

- Shambhavi Shinde January 31, 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