Microsoft Interview Question


Country: India
Interview Type: Written Test




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

Using the following "definition" of complete tree:

A binary tree is complete if the leaf nodes are only at the last level, and that too on the left, without any gaps.

The idea is to do a breadth first search while maintaining a number n with each node. And when we enque its children, we give the left child number 2n and right child 2n+1.

If the BFS does not return numbers in order 1,2,3 ..., M. Then it is not a complete tree. Otherwise it is.

Pseudo Code:

bool checkCompleteTree (Tree root) {
  
    if (!root) return true;
    Queue <Pair <root, int>> q;
    q.Enque(new Pair(root, 1));  
    int prevNumber = 0;
   
    while(!q.isEmpty()) {
        Pair p = q.Deque();
        Tree node = p.first;
        int number = p.second;
        if (number != prev_number + 1) { 
            return false;
        }
        prev_number++;
        if (!node.Left && node.Right) {
            return false;
        }
        if (node.Left) {
            q.Enque(new Pair(node.Left, number*2));
        }
        if (node.Right) {
            q.Enque(new Pair(node.Right, number*2 + 1));
        }
    }
}

- Anonymous September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There must be a return true after the while loop.

- Anonymous September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And that must be Queue <Pair<Tree, int>> q, not <root, int>...

Probably more bugs.

- Anonymous September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And the definition is not exactly right.

It is full upto the second last level and the last level if incomplete has leaf nodes towards the left.

- Anonymous September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

we can do it using BFS idea.
for every node popped we have to just check it it has both left and right children. If for any node this is not true i.e either it has no children or 1 child then we see for all the left over nodes in the queue if they have any children or not. if anyone of them has a child then its not complete binary tree else it is.

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

int isComBinTree(node *root,int ht)
{
    if(ht==1)
        return 1;
    if(root->right==NULL || root->left==NULL)
        return 0;
    return isComBinTree(root->right,ht-1) && isComBinTree(root->left,ht-1);
}

Where ht is the height of the tree and leaf nodes are assumed to be sub trees with height 1.

- madhu September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

breath first using queue. record first encounter of null. after that if a node is poped, it is not complete binary tree. if the loop just finished without next node, it is complete binary tree.

- hanju September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One more way to answer the question is to compare height of left and right subtrees.
Do a post-order tree traversal and check: if in any node MinHeight(LeftSubtree) < MaxHeight(RightSubtree) retrun 'tree is not complete'

- Aleksey.M December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One more way to answer the question is to compare height of left and right subtrees.
Do a post-order tree traversal and check: if in any node MinHeight(LeftSubtree) < MaxHeight(RightSubtree) retrun 'tree is not complete'

- Aleksey.M December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The following code depends on definition that says in a complete binary tree all the nodes should have exactly 2 nodes (or) none.


boolean isCompleteBT(Node* root) {
If (*root != null) {
return ((*root->left == null && *root->right == null) || (*root->left != null && *root->right != null)) && isCompleteBT(*root->left) && isCompleteBT(*root->right);
}
return true;
}

- Mario September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is not the right definition.

- Anonymous September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Complete binary tree is a tree which contains elements, such that if put in an array representation of ll there wont be any empty cell between the first and last data.

- n0va September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just did some pointer correction, otherwise this is correct.


boolean isCompleteBT(Node* root) {
If (root != null) {
return ((root->left == null && root->right == null) || (root->left != null && root->right != null)) && isCompleteBT(root->left) && isCompleteBT(root->right));
}
return true;
}

- Abhishek S September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the code you've written is for a proper binary tree.

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

- kenny September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

the min and max depth should be the same

- monk September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

using the definition a tree of height h will have 2^h-1 nodes in it...count the number of nodes using any traversal using stack coz better then recursion and then find height of the tree then chk..

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

Hi,
Its between 2^h to 2^(h-1) - 1. So count the nodes and if it is between these then it is complete. else false.

- Raghav October 23, 2012 | 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