Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

BFS Search with track of count on each iteration, the biggest value is answer.
Solition is O(n)

- Anna September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does not the postorder traversal solve the problem and it would be O(n) ?

- 29.abhishek.mittal September 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are three cases to consider when trying to find the longest path between two nodes in a binary tree (diameter):

The longest path passes through the root,
The longest path is entirely contained in the left sub-tree,
The longest path is entirely contained in the right sub-tree.
The longest path through the root is simply the sum of the heights of the left and right sub-trees + 1 (for the root node), and the other two can be found recursively:

public static int getDiameter(BinaryTreeNode root) {        
    if (root == null)
        return 0;

    int rootDiameter = getHeight(root.getLeft()) + getHeight(root.getRight()) + 1;
    int leftDiameter = getDiameter(root.getLeft());
    int rightDiameter = getDiameter(root.getRight());

    return Math.max(rootDiameter, Math.max(leftDiameter, rightDiameter));
}

public static int getHeight(BinaryTreeNode root) {
    if (root == null)
        return 0;

    return Math.max(getHeight(root.getLeft()), getHeight(root.getRight())) + 1;
}

- Snehal Masne November 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is O(n^2)

- Nikhil Kothari July 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude...

stackoverflow . com/ a/ 11897490

- cool.fire.ra July 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

2 methods to find diameter in O(n)

1. use hashing: store height of every node in hash table -> O(2n)

map <node*,int> h;
int height(struct node* node)
{
if(node == NULL)
return 0;
	int a=height(node->left);
	int b=height(node->right);
h[node]= 1 + max(a,b);
return h[node];
}

int diameter(struct node * tree)
{
if (tree == 0)
return 0;
int lheight = h[tree->left];
int rheight = h[tree->right];
int ldiameter = diameter(tree->left);
int rdiameter = diameter(tree->right);
return max(lheight + rheight + 1, max(ldiameter, rdiameter));
}

2. use postorder traversal in height fuction and keep updating value of diameter to leftheight + rightheight +1 -> O(n)

int dia(node* root,int *d){
	int lheight, rheight;
	if(!root) return 0;
	lheight=dia(root->left,d);
	rheight=dia(root->right,d);
	if(lheight+rheight+1>*d) *d=lheight+rheight+1;
	return 1+ max(lheight,rheight);
}

int diameter(node* root){
	int d=0;
	dia(root,&d);
	return d;
}

- JerryGoyal April 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be solved in O(n) with one traversal

public class Solution {
    private int res=0;
    private int helper(TreeNode root) {
        if(root==null){
            return -1;
        }
        int l=1+helper(root.left);
        int r=1+helper(root.right);
        res=Math.max(res,l+r);
        return Math.max(l,r);
    }
    public int diameterOfBinaryTree(TreeNode root){
        if(root==null)  return 0;
        helper(root);
        return res;
    }
}

- deerishi May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Write a recursive solution,
int diameter(Node *n)
{
if(n==NULL) return 0;
else return max( diameter(n->left), diameter(n->right), height(n->left) + height(n->right) + 1);
}

- Neeraj September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think this algorithm is not O(n) but O(n^2). Following code should help:

int diameter(Node *root, int *height)
{
  if (root==NULL)
  {
    *height=0;
    return 0
  }
  int ld, rd, lh, rh;
  ld=rd=lh=rh=0;
  ld = diameter(root->left, &lh);
  rd = diameter(root->right, &rh);
  *height=max(lh,rh)+1;
  return max(ld, max(rd, lh+rh+1));
}

- ACP Pradyuman September 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you store the height of the node, then it will be O(n)....height() function is making it O(n^2)

- Neeraj September 09, 2011 | Flag


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