Amazon Interview Question for Software Engineer / Developers






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

Height of Binary Tree Iteratively

private static Integer height(BinaryTreeNode root) {
		BinaryTreeNode temp;
		Queue<BinaryTreeNode> q = new LinkedBlockingDeque<BinaryTreeNode>();
		q.add(root);
		q.add(new BinaryTreeNode(-1));
		int level = 0;
		while (!q.isEmpty()) {
			temp = q.poll();
			if (temp.getData() == -1) {
				if (!q.isEmpty())
					q.add(new BinaryTreeNode(-1));
				level++;
			} else {
				if (temp.getLeft() != null)
					q.add(temp.getLeft());
				if (temp.getRight() != null)
					q.add(temp.getRight());
			}
		}

		return level;
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice use of a level-order traversal. From Wikipedia, here is helpful pseudocode for level-order traversal:

levelorder(root)
  q ← empty queue
  q.enqueue(root)
  while (not q.isEmpty())
    node ← q.dequeue()
    visit(node)
    if (node.left ≠ null)
      q.enqueue(node.left)
    if (node.right ≠ null)
      q.enqueue(node.right)

- johndifini November 06, 2016 | Flag
Comment hidden because of low score. Click to expand.
9
of 9 vote

Height Recursively

	private static Integer heightRecursively(BinaryTreeNode root) {
		int leftHeight, rightHeight;
		if (root == null)
			return 0;
		else {
			leftHeight = heightRecursively(root.getLeft());
			rightHeight = heightRecursively(root.getRight());
			if (leftHeight > rightHeight)
				return leftHeight + 1;
			else
				return rightHeight + 1;
		}
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Height(T) = 1 + max(Height(T->left), Height(T->right))

- Messi April 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

besides recursion way, traverse the binary tree by breath-first.
use a queque as an auxiliary and an empty tag.
queue.push(root)
queue.push(tag)
height = 0;
while(!queue.empty())
{
Node node = queue.pop();
if (node.isTag())
{
++height;
queue.push(node);
}
else
{
if (node.left != null)
queque.push(node.left);
if (node.right != null)
queue.push(node.right)
}
}

- wangminbyxy April 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

great :D

- AmitM February 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Binary_Tree_height(struct tree * node)
{
if(node==NULL)
return 0;
else
return(Binanry_Tree_height(node->left)+Binary_Tree_height(node->right)+1);
}

- Anonymous May 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are calculating number of nodes in BST, not height of BST

- Naga Samrat Chowdary, Narla May 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you should return 1+ greater_of( Binary_Tree_height(node->left)
, Binary_Tree_height(node->right) )

- Ashrith June 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int BinaryTreeHeight(struct node * node){
if (node == NULL) return 0;
lheight=BinaryTreeHeight(node->letf);
rheight=BinaryTreeHeight(node->right);
if (lheight>rheight)
return (lheight+1);
else
return (rheight+1);
}}

- perk May 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
        Recursive algorithm to find height (or depth) of binary tree
        O(n) time and space complexity.
        Note: Assumes rooted tree with single node is height of 1.
     */
    public static int binaryTreeHeight(BinaryTree.Node<Integer> root)
    {
          int leftHeight, rightHeight = 0;
          if (root == null)
          {
              return 0;
          } else {
              leftHeight = binaryTreeHeight(root.left);
              rightHeight = binaryTreeHeight(root.right);
              //max of the heights of two children + 1
              //instead of using Math.max
              //or could do 1 + max(Height(T->left), Height(T->right))
              if (leftHeight > rightHeight)
              {
                  return (leftHeight + 1);
              } else {
                  return (rightHeight + 1);
              }
          }

    }
    /*
    uses level-order traversal (breadth-first traversal). end of level tag is null pointer.
    Note: Assumes root height starts at 1.
    O(n) time and space complexity
     */
    public static int binaryTreeHeightIterative(BinaryTree.Node<Integer> root)
    {
        int level = 0;
        Queue<BinaryTree.Node<Integer>> queue = new LinkedList<BinaryTree.Node<Integer>>();
        if (root == null)
        {
            return 0;
        }
        queue.add(root);
        //END of first level
        queue.add(null);
        while (!queue.isEmpty())
        {
            root = queue.poll();


            //completion of CURRENT level
            if (root == null)
            {
                //put another marker for next level
                if (!queue.isEmpty())
                {
                    queue.add(null);

                }
                //increase level

                level++;
            } else {
                if (root.left != null)
                {
                   queue.add(root.left);

                }
                if (root.right != null)
                {
                    queue.add(root.right);
                }
            }
        }
        return level;
    }

- Anonymous December 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int height(struct node *p)
{
int l=0,r=0;
if(p->left != NULL)
l = 1 + height(p->left);
if(p->right != NULL)
r = 1 + height(p->right);
if(l>r) return l ;
else return r;
}

- Anonymous April 17, 2010 | 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